Created
March 28, 2015 00:26
-
-
Save jungchris/b1c7cda0b4e33faebcbe to your computer and use it in GitHub Desktop.
iOS Code posting a JSON message to Slack using a Webhook
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)postJSONToSlack { | |
// This is the URL to your Slack team and channel | |
NSMutableURLRequest *request = [NSMutableURLRequest | |
requestWithURL:[NSURL URLWithString:@"https://hooks.slack.com/services/YOUR_SLACK_WEBHOOK_ID_HERE"]]; | |
NSDictionary *requestData = [[NSDictionary alloc] initWithObjectsAndKeys: | |
@"Test post to NMDevs from iOS.\nSlack Webhook integration.", @"text", | |
nil]; | |
NSError *error; | |
NSData *postData = [NSJSONSerialization dataWithJSONObject:requestData options:0 error:&error]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[request setHTTPMethod:@"POST"]; | |
[request setHTTPBody:postData]; | |
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; | |
if (connection) { | |
NSLog(@"Connection Successful"); | |
} else { | |
NSLog(@"Connection could not be made"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment