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.

Try this:

  NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
 NULL,
 (CFStringRef)unencodedString,
 NULL,
 (CFStringRef)@"!*'();:@&=+$,/?%#[]",
 kCFStringEncodingUTF8 );

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.