Sending SMS via Twilio on ios?

How can I send SMS via twilio , I have already tried and do the 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);
    }     
 }

and got an error: the operation could not be completed. (error kCFErrorDomainCFNetwork -1012. Please help solve this problem or share help with me. Thanks in Advance.

+3
source share
3 answers

According to this response, error 1012 means that the authentication request was canceled by the user.

, HTTP Basic Auth, : Objective-c HTTP URL, Objective C, .

+4

, HttpPost URL-,

NSString *kToNumber = @"+14126620408";

NSString *kToNumber = @"%2B14126620408";
+4

, , Xcode 8 Swift 3.

https://www.twilio.com/blog/2016/11/how-to-send-an-sms-from-ios-in-swift.html

Alamofire HTTP-, :

@IBAction func sendData(sender: AnyObject) { 
    let headers = [
        "Content-Type": "application/x-www-form-urlencoded"
    ]

    let parameters: Parameters = [
        "To": phoneNumberField.text ?? "",
        "Body": messageField.text ?? ""
    ]

    Alamofire.request("YOUR_NGROK_URL/sms", method: .post, parameters: parameters, headers: headers).response { response in
            print(response)

    }
}
0

All Articles