How to decode URL string on an iOS

Often times we need to deal with URL encoded string. This is quite awkward to use for an UI. In order to fix this issue, we often tend to decode it.

Today we will see how to decode an URL string on an iOS

Say, NSString* inputURLString is an URL string you want to decode. It can be achieved as follows

For convenience purpose, I have made a category NSString+DecodeString on NSString


- (NSString*)decodeString {
       return (__bridge NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef) self, CFSTR(""), kCFStringEncodingUTF8);
}

As simple as it could get. You can replace self with NSString parameter if you want to pass it as a method parameter instead of creating a category.

Jayesh Kawli

I am a web and mobile developer working at Wayfair in Boston, MA. I come to learn so many things during course of life and I write about things which helped me and feel like they can help others too.

Subscribe to Fresh Beginning

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!