Inviare un’API WhatsApp utilizzando Objective-C

Introduzione

In questo snippet di codice, imparerete come inviare un’API WhatsApp utilizzando Objective-C, per inviare un messaggio di testo, modelli WhatsApp, media, pulsanti interattivi, elenchi, prodotti … ecc, è necessario un account Alvochat per eseguire i seguenti codici. Create un account se non ne avete uno.

Inviare un modello utilizzando l’API di WhatsApp

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/template"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&name=hello_world" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&language=en_us" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&header=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&buttons=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Inviare un messaggio di testo utilizzando l’API di WhatsApp

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/chat"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body=WhatsApp API on alvochat.com works good" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&preview_url=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Invia immagine

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/image"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&image=https://alvochat-example.s3-accelerate.amazonaws.com/image/1.jpeg" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&caption=image caption" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Inviare l’audio

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/audio"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&audio=https://alvochat-example.s3-accelerate.amazonaws.com/audio/1.mp3" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Invia il video

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/video"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&video=https://alvochat-example.s3-accelerate.amazonaws.com/video/1.mp4" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&caption=video caption" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Inviare il documento

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/document"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&document=https://alvochat-example.s3-accelerate.amazonaws.com/document/1.pdf" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&filename=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Invia l’adesivo

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/sticker"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&sticker=https://alvochat-example.s3-accelerate.amazonaws.com/sticker/1.webp" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Inviare il contatto

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/contact"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&contact=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Invia posizione

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/location"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&lat=37.484296" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&lng=-122.148703" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&address=Menlo Park, California, United States" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&name=Meta Headquarters" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Inviare l’elenco

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/list"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&header=header" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body= please select one of the following options" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&footer=footer" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&button=options" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"§ions=option_1,option_2,option_3" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Pulsante di invio

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/button"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&header=header" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body= please select one of the following options" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&footer=footer" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&buttons=option_1,option_2,option_3" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Inviare i prodotti

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/product"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&header=header" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body=Hi , check out our new products" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&footer=footer" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&catalog_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&product=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

È possibile consultare la documentazione completa dell’API di Whatsapp qui.