CFURLCreateStringByAddingPercentEscapes is deprecated in iOS 9, how do I use "stringByAddingPercentEncodingWithAllowedCharacters",

I have the following code:

return (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(";:@&=+$,/?%#[]"), kCFStringEncodingUTF8); 

Xcode says it's deprecated in iOS 9. So, how can I use stringByAddingPercentEncodingWithAllowedCharacters?

Thanks!

+5
source share
3 answers

try it

 NSString *value = @"<url>"; value = [value stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; 
+18
source

The character set URLQueryAllowedCharacterSet contains all the characters allowed in the query part of the URL ( ..?key1=value1&key2=value2 ), and is not limited to the characters allowed in the keys and values โ€‹โ€‹of such a query. For instance. URLQueryAllowedCharacterSet contains & and + , since they are of course allowed in the request ( & separates key / value pairs and + means space), but they are not allowed in the key or the value of such a request.

Consider this code:

 NSString * key = "my&name"; NSString * value = "test+test"; NSString * safeKey= [key stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet] ]; NSString * safeValue= [value stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet] ]; NSString * query = [NSString stringWithFormat:@"?%@=%@", safeKey, safeValue]; 

query will be ?my&name=test+test , which is completely wrong. It defines a key with the name my , which does not matter, and a key with the name name , whose value is test test (+ means a space!).

The correct query would be ?my%26name=test%2Btest .

As long as you use only ASCII strings or as long as the server can deal with UTF-8 characters in the URL (most web servers do this today), the number of characters you absolutely must encode is actually quite small and very constant. Just try this code:

 NSCharacterSet * queryKVSet = [NSCharacterSet characterSetWithCharactersInString:":/?&=; +!@ #$()',*% " ].invertedSet; NSString * value = ...; NSString * valueSafe = [value stringByAddingPercentEncodingWithAllowedCharacters:queryKVSet ]; 
+9
source

Another solution for encoding these characters is allowed in the URLQueryAllowedCharacterSet but not allowed in the key or value (for example: + ):

 - (NSString *)encodeByAddingPercentEscapes { NSMutableCharacterSet *charset = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy]; [charset removeCharactersInString:@"!*'();:@&=+$,/?%#[]"]; NSString *encodedValue = [self stringByAddingPercentEncodingWithAllowedCharacters:charset]; return encodedValue; } 
+1
source

Source: https://habr.com/ru/post/1244409/


All Articles