NSString URL decode?

Geri Borbás picture Geri Borbás · Mar 27, 2013 · Viewed 22.7k times · Source

I have tried a lot of approaches out there, but this tiny little string just cannot be URL decoded.

NSString *decoded;
NSString *encoded = @"fields=ID%2CdeviceToken";
decoded = (__bridge NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (CFStringRef)encoded, NULL, NSUTF8StringEncoding);
NSLog(@"decodedString %@", decoded);

The code above just logs the same (!) string after replacing percent escapes.

Is there a reliable solution out there? I think some kind of RegEx solution based on some documentation could work. Any suggestion?

Answer

rmaddy picture rmaddy · Mar 27, 2013

Another option would be:

NSString *decoded = [encoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];