Skip to content

Instantly share code, notes, and snippets.

@krhoyt
Last active December 27, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krhoyt/7359097 to your computer and use it in GitHub Desktop.
Save krhoyt/7359097 to your computer and use it in GitHub Desktop.
Send a text message using the Twilio service.
- ( void ) sendTwilioTextMessage : ( NSString * ) toNumber withBody : ( NSString * ) body
{
NSArray * items;
NSData * post;
NSError * error;
NSHTTPURLResponse * response;
NSMutableURLRequest * request;
NSString * contents;
NSXMLDocument * doc;
// Message details
contents = [[NSString alloc] initWithFormat:@"From=_TWILIO_PHONE_NUMBER_&To=%@&Body=%@", toNumber, body];
// Send form data
request = [[NSMutableURLRequest alloc] init];
[request setURL : [NSURL URLWithString : @"https://_ACCOUNT_SID_:_ACCOUNT_AUTH_TOKEN_@api.twilio.com/2010-04-01/Accounts/_ACCOUNT_SID_/SMS/Messages"]];
[request setHTTPMethod : @"POST"];
[request setValue : [NSString stringWithFormat : @"%lu", [contents length]] forHTTPHeaderField : @"Content-Length"];
[request setValue : @"application/x-www-form-urlencoded" forHTTPHeaderField : @"Content-Type"];
[request setHTTPBody : [contents dataUsingEncoding : NSUTF8StringEncoding]];
error = [[NSError alloc] init];
response = nil;
post = [NSURLConnection sendSynchronousRequest : request returningResponse : &response error : &error];
doc = [[NSXMLDocument alloc] initWithData : post options : ( NSXMLNodePreserveWhitespace | NSXMLNodePreserveCDATA ) error : &error];
items = [doc nodesForXPath:@"//DateCreated" error : &error];
NSLog( @"%@", [[items objectAtIndex:0] objectValue] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment