我如何通过 twilio 发送短信,我已经尝试过并正在做以下事情。
- (IBAction)sendButtonPressed:(id)sender
{
NSLog(@"Sending request.");
// Common constants
NSString *kTwilioSID = delegate.sessionId;
NSString *kTwilioSecret = delegate.twilioToken;
NSString *kFromNumber = delegate.twlioNumber;
NSString *kToNumber = @"+14126620408";
NSString *kMessage = @"Hi there......";
// Build request
NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];
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=%@", kFromNumber, kToNumber, kMessage];
NSData *data = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
NSError *error;
NSURLResponse *response;
NSData *receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// Handle the received data
if (error) {
NSLog(@"Error: %@", error);
} else {
NSString *receivedString = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"Request sent. %@", receivedString);
}
}
并得到错误:操作无法完成。(kCFErrorDomainCFNetwork 错误 -1012。请帮助解决此问题,或与我分享任何帮助材料。在此先感谢。