How to URL Decode in iOS – Objective C

The stringByReplacingPercentEscapesUsingEncoding method is not working properly as it’s not decoding special symbols that dont start with a % character, i.e., the + character. Does anyone know of a better method to do this in iOS?

Here’s what I’m currently using:

NSString *path = [@"path+with+spaces"

And here’s an example of the output:


4 Solutions Collect From Internet About “How to URL Decode in iOS – Objective C”

NSString *path = [[@"path+with+spaces"
    stringByReplacingOccurrencesOfString:@"+" withString:@" "]

Note that the plus-for-space substitution is only used in application/x-www-form-urlencoded data – the query string part of a URL, or the body of a POST request.

// Decode a percent escape encoded string.
- (NSString*) decodeFromPercentEscapeString:(NSString *) string {
return (__bridge NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                        (__bridge CFStringRef) string,

This seems to be the preferred way because…
“Apparently” this is a “bug” apple is aware of, but they haven’t done anything about it yet… ( )

If you are trying to replace the plus sign with percent escapes, perform a string replacement from “+” to ” ” (single space). Then use stringByAddingPercentEscapesUsingEncoding: to add the percent escapes.

The plus sign is one of many reserved URL characters that is never encoded.

(stringByReplacingPercentEscapesUsingEncoding: decodes the percent escapes)

swift 2 :

extension String  {

    func uriDecodedString() -> String? {
       return self.stringByReplacingOccurrencesOfString("+", withString: " ").stringByRemovingPercentEncoding