How to really URL encode an NSString in Objective-C, iPhone, etc.
Posted on April 22, 2009 at 02:52 AM
Categories: code, mac, iphone, objectivec
Trying to encode URL parameters on Mac or iPhone? Frustrated because NSString stringByAddingPercentEscapesUsingEncoding encodes non-URL characters but leaves the reserved characters (like slash / and ampersand &) alone? "Apparently" this is a "bug" apple is aware of, but they haven't done anything about it yet, and so, here is a solution that actually works.
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
As an example, @"'Decoded data!'/foo.bar:baz" will become "%27Decoded%20data%21%27%2Ffoo.bar%3Abaz".
Obviously you would use this, not on the full URL, but just on the parameters.