我正在寻找一个在线短信发送服务,我可以使用我的iOS-应用程序。我的目的是发送一个验证代码到电话号码,用户提供。在安卓系统,这不是什么大问题,因为我可以通过编程发送短信,但在iOS,我不能。有什么建议吗?
发布于 2014-03-30 15:53:13
在我们的应用程序中,我们使用http://www.twilio.com/通过iPhone上的服务发送短信,这非常棒。
对于发送SMS,您甚至不需要下载它们的SDK并将其放入项目中。它基本上可以归结为HTTP命令,在该命令中,您向SMS、消息正文和API key+secret提供电话号码。
下面是一个通过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);
}
}];
}https://stackoverflow.com/questions/22746030
复制相似问题