我怎么能够通过 twilio 发送短信 https://www.twilio.com/docs/api/rest,我已经尝试过并正在执行以下操作。
- (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。
请帮助解决这个问题,或者与我分享任何帮助材料。提前致谢。
根据这个答案 https://stackoverflow.com/a/3798846/329700,错误 1012 表示用户取消了身份验证请求。
这只是一种预感,但您可能想通过添加如下授权标头来尝试使用 HTTP 基本身份验证:Objective-c HTTP 基本身份验证 https://stackoverflow.com/questions/4120311/objective-c-http-basic-authentication而不是在 URL 字符串中包含凭据,这依赖于 Objective C 库将它们正确地转换为标头。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)