3
votes

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);
    }     
 }

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.

4
I am using Your code to send SMS in iOS application. But i am getting a different error than you while sending the SMS. The 'To' number +91********** is not a valid phone number(Code 21211) I have checked that the number i am using is properly defined with +(Country Code)(Phone No). Can you help me in this problem?Rachit
Hi did you solve this problemSurender Rathore

4 Answers

4
votes

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

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.

4
votes

Make sure that the HttpPost param is URL-encoded, so you should change

NSString *kToNumber = @"+14126620408";

to

NSString *kToNumber = @"%2B14126620408";
0
votes

I've written this blog post to help you get this done quickly using Xcode 8 and Swift 3.

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

Using a server-side language of your choosing and Alamofire for HTTP requests, the request boils down to this:

@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
votes

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

let parameters: Parameters = [
    "To":  "enter your number",
    "Body":  "enter your text"
]

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

}

}

you must be installed TIP: pod 'Alamofire', '~> 5.2'