Reputation: 1078
I'm looking for an online sms sending service, that I can use within my iOS-App. My aim is to send a verification code to the phone number, the user provided. In Android its not too big of a deal, since I can send sms programmatically, but in iOS I can't. Any suggestions?
Upvotes: 1
Views: 441
Reputation: 3012
In our app we use http://www.twilio.com/ to send SMS through a service on the iPhone, its pretty great.
For sending SMSs you don't even need to download their SDK and put it into your project. It basically comes down to a HTTP POST command in which you provide the phone number to SMS, the message body, and your API key+secret.
Here is an example for your convenience on sending an SMS through Twillio:
- (void)twilloSendSMS:(NSString *)message withQueue:(NSOperationQueue *)queue
andSID:(NSString *)SID andSecret:(NSString *)secret
andFromNumber:(NSString *)from andToNumber:(NSString *)to {
NSLog(@"Sending request.");
// Build request
NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", SID, secret, SID];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
// Set up the body
NSString *bodyString = [NSString stringWithFormat:@"From=%@&To=%@&Body=%@", from, to, message];
NSData *data = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[NSURLConnection
sendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ([data length] >0 && error == nil && ([httpResponse statusCode] == 200 || [httpResponse statusCode] == 201))
{
NSString *receivedString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Request sent. %@", receivedString);
}
else {
NSString *receivedString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Request sent. %@", receivedString);
NSLog(@"Error: %@", error);
}
}];
}
Upvotes: 5