URL Encoding in Objective-C

The NSString class contains a method stringByAddingPercentEscapesUsingEncoding which returns a URL-encoded string. Problem is, the method seems to provide support for percent escapes on just a few characters.

Below is a category extension to the NSString class that uses the method CFURLCreateStringByAddingPercentEscapes in the CFURL class to handle a more complete set of characters for encoding. By using a category (versus a standalone method in a class) we are essentially adding a new method for encoding to all NSString objects.

Continue reading