如何从iPhone应用程序以编程方式发送短信?我现在正在使用Twilio,并且可以正确设置HTTP请求,与服务器进行身份验证并获得响应。在iOS上发送短信与Twilio
由于我可以从Twilio服务器获得响应,但是从不传递正确的数据,所以必须存在HTTP头的某些配置错误。
我目前的代码是在一个简单的按钮按下的方法。
- (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);
我认为问题在于你应该使用POST字段,而不是HTTP标题......我将验证那真的很快...... –
请注意,不建议使用Twilio API直接从iOS应用程序发送短信,因为您必须在应用程序中嵌入您的AccountSid和AuthToken,该应用程序可以很容易地反编译妥协您的凭据。使用Parse或Azure移动服务等服务来托管一个简单的服务来发送消息并从您的应用程序调用该服务会更安全。 –
Devin,即使使用Parse或Azure,您也必须将您的授权令牌放入已编译的应用程序中。 –