How can I send an SMS message programatically from an iPhone app? I’m using Twilio right now, and can correctly set up a HTTP Request, authenticate with the server, and get a response.
There must be some misconfiguration of the HTTP Headers as I can get a response from the Twilio servers but never passes the right data through.
My current code is in a method that’s called by a simple button press.
- (IBAction)sendButtonPressed:(id)sender {
NSLog(@"Button pressed.");
NSString *kYourTwillioSID = @"AC8c3...f6da3";
NSString *urlString = [NSString stringWithFormat:@"https://AC8c3...6da3:bf...0b7@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kYourTwillioSID];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setValue:@"+18584334333" forHTTPHeaderField:@"From"];
[request setValue:@"+13063707780" forHTTPHeaderField:@"To"];
[request setValue:@"Hello\n" forHTTPHeaderField:@"Body"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSString *response_details = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",response_details);
}
NSLog(@"Request finished %@", error);
If you are just looking to send an SMS message in iOS you can use the
MFMessageComposeViewControllerinside of theMessageUI.framework. As you know though, this requires user-interaction.As you had requested, you can use Twilio to send SMS directly using almost any platform. For iOS you can use the following Swift code to hit the Twilio API and send any text messages you’d like:
For any further API interaction you can check out the official docs: https://www.twilio.com/docs/api/rest