如何从 iPhone 应用程序以编程方式发送 SMS 消息?我现在正在使用 Twilio,并且可以正确设置 HTTP 请求、向服务器进行身份验证并获得响应。
HTTP 标头一定存在一些错误配置,因为我可以从 Twilio 服务器获得响应,但从未传递正确的数据。
我当前的代码是通过简单的按钮按下调用的方法。
- (IBAction)sendButtonPressed:(id)sender {
NSLog(@"Button pressed.");
NSString *kYourTwillioSID = @"AC8c3...f6da3";
NSString *urlString = [NSString stringWithFormat:@"https://AC8c3...6da3:bf...0b7@api.twilio.com/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);