Contents
hide
Введение
В этом фрагменте кода вы узнаете, как отправить WhatsApp API с помощью Objective-C, чтобы отправить текстовое сообщение, WhatsApp Шаблоны, медиа, интерактивные кнопки, списки, продукты … и т.д., Для выполнения следующих кодов требуется учетная запись Alvochat. Создайте аккаунт, если у вас его нет.
Отправка шаблона с помощью WhatsApp API
#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);
Отправка текстового сообщения с помощью API 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);
Отправить изображение
#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);
Отправить аудио
#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);
Отправить видео
#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);
Отправить документ
#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);
Отправить наклейку
#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);
Отправить контакт
#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);
Отправить местоположение
#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);
Список отправки
#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);
Кнопка отправки
#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);
Отправить продукцию
#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);
Вы можете посмотреть полную документацию Whatsapp API здесь.