NSString URL decode?
Asked Answered
C

4

19

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?

Cassicassia answered 27/3, 2013 at 3:44 Comment(1)
for swift let decoded = encoded!.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding);Objectivity
L
18

Use CFSTR("") instead of NULL for the second to last argument. From the CFURL reference:

charactersToLeaveEscaped

Characters whose percent escape sequences, such as %20 for a space character, you want to leave intact. Pass NULL to specify that no percent escapes be replaced, or the empty string (CFSTR("")) to specify that all be replaced.

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

Prints:

2013-03-26 21:48:52.559 URLDecoding[28794:303] decodedString fields=ID‭,‬deviceToken

Lichter answered 27/3, 2013 at 3:49 Comment(1)
@Carl When I try your code it prints Decoded String : (null) in console. I found the reason. Instead of NSUTF8StringEncoding , I used kCFStringEncodingUTF8 and it works.Martynne
G
34

Another option would be:

NSString *decoded = [encoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Graph answered 27/3, 2013 at 3:52 Comment(2)
I still have percents in mine after doing this for example. http%3A%2F%2Fwebsite.comAirs
@Airs This code works for your example in iOS10.1, which system version did you use then? I think implementation of the interface has changed.Ashcan
L
18

Use CFSTR("") instead of NULL for the second to last argument. From the CFURL reference:

charactersToLeaveEscaped

Characters whose percent escape sequences, such as %20 for a space character, you want to leave intact. Pass NULL to specify that no percent escapes be replaced, or the empty string (CFSTR("")) to specify that all be replaced.

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

Prints:

2013-03-26 21:48:52.559 URLDecoding[28794:303] decodedString fields=ID‭,‬deviceToken

Lichter answered 27/3, 2013 at 3:49 Comment(1)
@Carl When I try your code it prints Decoded String : (null) in console. I found the reason. Instead of NSUTF8StringEncoding , I used kCFStringEncodingUTF8 and it works.Martynne
F
14

CFURLCreateStringByReplacingPercentEscapesUsingEncoding is deprecated in iOS 9. Use stringByRemovingPercentEncoding instead.

NSString *decoded = [encoded stringByRemovingPercentEncoding];
Footage answered 21/12, 2015 at 8:23 Comment(0)
M
1

Swift 3

import Foundation

let charSet = CharacterSet.urlPathAllowed.union(.urlQueryAllowed) // just use what you need, either path or query
let enc = "Test Test Test".addingPercentEncoding(withAllowedCharacters: charSet)!
let dec = enc.removingPercentEncoding!

print("Encoded: \(enc)")
print("Decoded: \(dec)")

Output:

Encoded: Test%20Test%20Test

Decoded: Test Test Test

Moony answered 17/2, 2017 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.