Cómo enviar una API de WhatsApp con Objective-C

Introducción

En este tutorial, crearemos ejemplos simples para enviar mensajes a través de la API de WhatsApp utilizando Objective-C.

Primer mensaje de la API de WhatsApp usando Objective-C


#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body=WhatsApp API on UltraMsg.com works good" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=10" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/chat"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

Enviar imagen

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&image=https://file-example.s3-accelerate.amazonaws.com/images/test.jpg" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&caption=image Caption" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/image"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

Enviar documento

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&filename=hello.pdf" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&document=https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/document"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

Enviar sonido

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&audio=https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/audio"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

Enviar voz

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&audio=https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/voice"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

Enviar vídeo

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&video=https://file-example.s3-accelerate.amazonaws.com/video/test.mp4" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/video"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];
#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&link=https://en.wikipedia.org/wiki/COVID-19" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/link"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

Enviar contacto

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&[email protected]" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/contact"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

Enviar ubicación

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&address=ABC company \n Sixth floor , office 38" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&lat=25.197197" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&lng=55.2721877" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/location"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

Enviar tarjeta virtual

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&vcard= BEGIN:VCARD\nVERSION:3.0\nN:lastname;firstname\nFN:firstname lastname\nTEL;TYPE=CELL;waid=14000000001:14000000002\nNICKNAME:nickname\nBDAY:01.01.1987\nX-GENDER:M\nNOTE:note\nADR;TYPE=home:;;;;;;\nADR;TYPE=work_:;;;;;;\nEND:VCARD" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/vcard"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

finalmente, puede ver la documentación completa de la API de Whatsapp y las preguntas frecuentes .