问题描述
我如何通过twilio发送短信,我已经尝试过并执行以下操作.
How can i send SMS through twilio, i have tried already and doing following.
- (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.请帮助解决此问题,或与我分享任何帮助材料.预先感谢.
and got error: The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.Please help to do this issue, or share with me any helping meterial. Thanks in Advance.
推荐答案
根据此答案,错误1012表示用户取消了身份验证请求.
According to this answer, error 1012 means that a request for authentication was canceled by the user.
这只是一种预感,但是您可能想尝试通过添加如下所示的Authorization标头来尝试使用HTTP Basic Auth:,而不是在URL字符串中包括凭据,而该字符串依靠Objective C库将它们正确地转换为标头.
It's just a hunch, but you may want to try using HTTP Basic Auth by adding an Authorization header like this: Objective-c HTTP Basic authentication instead of including the credentials in the URL string, which counts on the Objective C library to turn those into a header correctly.
这篇关于在iOS上通过Twilio发送短信吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!