pengenalan
Dalam coretan kod ini, anda belajar cara menghantar API WhatsApp menggunakan Objective-C, untuk menghantar mesej teks, Templat WhatsApp, media, butang interaktif, senarai, produk … dll, akaun Alvochat diperlukan untuk menjalankan kod berikut. Buat Akaun jika anda tidak mempunyainya.
Hantar templat menggunakan 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/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);
Hantar mesej teks menggunakan 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);
Hantar imej
#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);
Hantar 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);
Hantar 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);
Hantar dokumen
#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);
Hantar pelekat
#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);
Hantar Kenalan
#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);
Hantar Lokasi
#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);
Hantar senarai
#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);
Butang hantar
#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);
Hantar produk
#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);
Anda boleh melihat Dokumentasi API Whatsapp Penuh di sini .