29

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"
     stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

And here's an example of the output:

path+with+spaces

VinnyD
  • 3,500
  • 9
  • 34
  • 48
  • 3
    Can you please post your code or an example of where and how that method is not working properly for you? – Carter Oct 27 '11 at 17:54
  • but what i'm trying to do is apply a php-like urldecode() function that will recover the original structure of the file names. – VinnyD Oct 27 '11 at 18:36

5 Answers5

53
NSString *path = [[@"path+with+spaces"
    stringByReplacingOccurrencesOfString:@"+" withString:@" "]
    stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 2
    there are other characters other that the + that need to be decoded, no? – VinnyD Oct 27 '11 at 18:33
  • When I tried to decode a string @"abcjhjhdfjhafjakhfjaklfj12346890(*^$#@@@#$%^^ ........", using this method, it returns nil. It looks like stringByReplacingPercentEscapesUsingEncoding is causing some problems because of "Returns nil if the transformation is not possible (i.e. the percent escapes give a byte sequence not legal in the given encoding). " (from NSURL.h). So just a heads up for those that ran into this issue making a category like me. – tony.tc.leung Nov 12 '14 at 21:33
  • 3
    `%^^` isn't a valid percent-escape. – rob mayoff Nov 12 '14 at 22:02
20
// Decode a percent escape encoded string.
- (NSString*) decodeFromPercentEscapeString:(NSString *) string {
return (__bridge NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                        (__bridge CFStringRef) string,
                                                        CFSTR(""),
                                                        kCFStringEncodingUTF8);
} 

http://cybersam.com/ios-dev/proper-url-percent-encoding-in-ios

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... ( http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ )

delux247
  • 2,177
  • 3
  • 21
  • 29
2

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)

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
1

swift 2 :

extension String  {

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

}
Mojtaba Yeganeh
  • 2,788
  • 1
  • 30
  • 49
0

Also you can use the PercentEncoder library from Cocoapods.

Swift 4

  • Include the library to your Podfile:

    pod PercentEncoder

  • Import the library PercentEncoder

    import PercentEncoder

    class ViewController{

    ...

    }

  • Replace the "+" character by "%20" and use the method "ped_decodeURI"

    "path+with+spaces".replacingOccurrences(of: "+", with: "%20").ped_decodeURI()

It will return "path with spaces"

Here the link for reference: https://cocoapods.org/pods/PercentEncoder