Skip to content

Instantly share code, notes, and snippets.

@hboon
Created December 4, 2012 16:37
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 hboon/4205927 to your computer and use it in GitHub Desktop.
Save hboon/4205927 to your computer and use it in GitHub Desktop.
Write a string to a Campfire room synchronously.
// This goes into the header file.
void moCampfireSpeak(NSString* aString, NSString* aRoomIDString, NSString* aSubdomainString, NSString* aTokenString);
// This goes into the implementation file.
// You need the room ID, not room name. You can get it from the campfire room URL.
void moCampfireSpeak(NSString* aString, NSString* aRoomIDString, NSString* aSubdomainString, NSString* aTokenString) {
NSString* url = [NSString stringWithFormat:@"https://%@.campfirenow.com/room/%@/speak.json", aSubdomainString, aRoomIDString];
NSString* postString = [NSString stringWithFormat:@"<message><type>%@</type><body>%@</body></message>", @"TextMessage", aString];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
NSString* auth = [NSString stringWithFormat:@"%@:%@", aTokenString, @"X"];
NSString* authValue = [NSString stringWithFormat:@"Basic %@", [[auth dataUsingEncoding:NSUTF8StringEncoding] base64EncodingWithLineLength:80]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
[request addValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse* response;
NSError* error;
NSData* res = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* result = [[NSString alloc] initWithData:res encoding:NSUTF8StringEncoding];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment