在 iOS 上使用 Twilio 发送短信
如何从 iPhone 应用程序以编程方式发送 SMS 消息?我现在正在使用 Twilio,并且可以正确设置 HTTP 请求、向服务器进行身份验证并获取响应。
HTTP 标头肯定存在一些错误配置,因为我可以从 Twilio 服务器获取响应,但从未传递正确的数据。
我当前的代码位于通过简单按下按钮即可调用的方法中。
- (IBAction)sendButtonPressed:(id)sender {
NSLog(@"Button pressed.");
NSString *kYourTwillioSID = @"AC8c3...f6da3";
NSString *urlString = [NSString stringWithFormat:@"https://AC8c3...6da3:[email protected]/2010-04-01/Accounts/%@/SMS/Messages", kYourTwillioSID];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setValue:@"+18584334333" forHTTPHeaderField:@"From"];
[request setValue:@"+13063707780" forHTTPHeaderField:@"To"];
[request setValue:@"Hello\n" forHTTPHeaderField:@"Body"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSString *response_details = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",response_details);
}
NSLog(@"Request finished %@", error);
How can I send an SMS message programatically from an iPhone app? I'm using Twilio right now, and can correctly set up a HTTP Request, authenticate with the server, and get a response.
There must be some misconfiguration of the HTTP Headers as I can get a response from the Twilio servers but never passes the right data through.
My current code is in a method that's called by a simple button press.
- (IBAction)sendButtonPressed:(id)sender {
NSLog(@"Button pressed.");
NSString *kYourTwillioSID = @"AC8c3...f6da3";
NSString *urlString = [NSString stringWithFormat:@"https://AC8c3...6da3:[email protected]/2010-04-01/Accounts/%@/SMS/Messages", kYourTwillioSID];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setValue:@"+18584334333" forHTTPHeaderField:@"From"];
[request setValue:@"+13063707780" forHTTPHeaderField:@"To"];
[request setValue:@"Hello\n" forHTTPHeaderField:@"Body"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSString *response_details = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",response_details);
}
NSLog(@"Request finished %@", error);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您只是想在 iOS 中发送短信,您可以使用
MessageUI.framework
内的MFMessageComposeViewController
。但正如您所知,这需要用户交互。正如您所要求的,您可以使用 Twilio 直接使用几乎任何平台发送短信。对于 iOS,您可以使用以下 Swift 代码来访问 Twilio API 并发送您想要的任何短信:
对于任何进一步的 API 交互,您可以查看官方文档:https://www.twilio.com/docs/api/rest
If you are just looking to send an SMS message in iOS you can use the
MFMessageComposeViewController
inside of theMessageUI.framework
. As you know though, this requires user-interaction.As you had requested, you can use Twilio to send SMS directly using almost any platform. For iOS you can use the following Swift code to hit the Twilio API and send any text messages you'd like:
For any further API interaction you can check out the official docs: https://www.twilio.com/docs/api/rest
使用AFNetworking发送请求。
Use AFNetworking to send request.
可能是这样的:
号码+YOURNUMBER 未经验证。试用账户无法向未经验证的号码发送消息;在twilio.com/user/account/phone-numbers/verified,或购买 Twilio 号码以向未经验证的号码发送消息。
It could be this:
The number +YOURNUMBER is unverified. Trial accounts cannot send messages to unverified numbers; verify +YOURNUMBER at twilio.com/user/account/phone-numbers/verified, or purchase a Twilio number to send messages to unverified numbers.
Twilio 与 Swift 2.2+、Alamofire、SwiftyJSON ->回答:
Twilio with Swift 2.2+, Alamofire, SwiftyJSON -> answer:
Xcode 8 和 Swift 3 的示例(已更新)。
https: //www.twilio.com/blog/2016/11/how-to-send-an-sms-from-ios-in-swift.html
我们不建议在客户端存储您的凭据,因此帖子展示如何使用您选择的服务器端语言和 HTTP 请求的 Alamofire 来避免潜在的漏洞:
An example (updated) for Xcode 8 and Swift 3.
https://www.twilio.com/blog/2016/11/how-to-send-an-sms-from-ios-in-swift.html
We don't recommend storing your credentials client side and so the post shows you how to avoid a potential vulnerability using a server-side language of your choosing and Alamofire for HTTP requests: