Last active
December 27, 2015 16:59
-
-
Save krhoyt/7359097 to your computer and use it in GitHub Desktop.
Send a text message using the Twilio service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- ( 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