Skip to content

Instantly share code, notes, and snippets.

@jungchris
Created March 28, 2015 00:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jungchris/b1c7cda0b4e33faebcbe to your computer and use it in GitHub Desktop.
Save jungchris/b1c7cda0b4e33faebcbe to your computer and use it in GitHub Desktop.
iOS Code posting a JSON message to Slack using a Webhook
- (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