【发布时间】:2014-05-09 20:59:21
【问题描述】:
我正在寻找可以在我的 iOS 应用程序中使用的在线短信发送服务。 我的目标是向用户提供的电话号码发送验证码。 在 Android 中这没什么大不了的,因为我可以通过编程方式发送短信,但在 iOS 中 我不能。有什么建议吗?
【问题讨论】:
我正在寻找可以在我的 iOS 应用程序中使用的在线短信发送服务。 我的目标是向用户提供的电话号码发送验证码。 在 Android 中这没什么大不了的,因为我可以通过编程方式发送短信,但在 iOS 中 我不能。有什么建议吗?
【问题讨论】:
在我们的应用程序中,我们使用http://www.twilio.com/ 通过 iPhone 上的服务发送短信,这非常棒。
对于发送 SMS,您甚至不需要下载他们的 SDK 并将其放入您的项目中。它基本上归结为一个 HTTP POST 命令,您可以在其中向 SMS 提供电话号码、消息正文和您的 API 密钥+秘密。
为了方便您通过 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);
}
}];
}
【讨论】: