Home
subscribe

L'etat, c'est moi

Mere Complexities sells the consulting and development services of me, Paul Wilson.

Conferences

Organising Scotland on Rails
Speaker, RailsConf Europe '08

Archive

Escaping Post Parameters in Cocoa/Cocoa-touch (iPhone)

So it turns out that you need to URL encode your Post parameters if you’re doing a HTTP Post from your Objective-C. Who knew? Well probably everyone who has a passing knowledge of rfc3896. I guess I’m just used to browsers and frameworks doing the work for me.

You’d think that the stringByAddingPercentEscapesUsingEncoding method on NSString would be just the ticket. Nope. The two characters you most need to escape, ampersand and plus (&+), are not escaped. What to do? Well you could copy this code I’ve just added to Twitterlink.


-(NSString*)currentMessageUrlEncoded{
  CFStringRef encoded = CFURLCreateStringByAddingPercentEscapes(
    kCFAllocatorDefault, 
    (CFStringRef) self.currentMessage, 
    nil, 
    @"&+", 
    kCFStringEncodingUTF8);  
  return [((NSString*) encoded) autorelease];
}

(Thanks to Jamie Montgomerie for confirming that I was doing the right thing with the autorelease).


Giles Bowkett Slapdown

Judging unit testing by its ability to find bugs is like judging your pet dog by his or her ability to spin webs and manufacture silk out of his or her ass.

Giles Bowkett commenting on a particularly idiotic blog post.