CFURLCreateStringByAddingPercentEscapes

I found that the CFURLCreateStringByAddingPercentEscapes method has been deprecated in iOS9.0. Below is my previous code:


// UTF-8-> GBK

-(NSData *) encodeStringUseGB_18030_2000: (NSString *) src {

// GBK + urlEncode

NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);

NSString * encodedString = (NSString *) CFBridgingRelease (CFURLCreateStringByAddingPercentEscapes (kCFAllocatorDefault, (CFStringRef) src, NULL, CFSTR (": /? # [] @! $ & '() * +,; ==" "> \\ ^ ~ `"), enc));

return [encodedString dataUsingEncoding: enc];

}


I used stringByAddingPercentEncodingWithAllowedCharacters to replace it according to the documentation, but I have not been able to get the correct results. What should I do to get the same result as the previous code?

Replies

I have not been able to get the correct results.

The big problem here is your definition of “correct results”. The older

CFURL
and
NSURL
APIs don’t necessarily conform to an obvious standard and, even where they do, they are built on top of old standards. Moreover, for
CFStringConvertEncodingToNSStringEncoding
you can override the standard behaviours via the
charactersToLeaveUnescaped
and
legalURLCharactersToBeEscaped
parameters, and I can see that’s what you’re doing here.

So, my question is, how do you define “correct results”? Are you trying to conform to some specific standard? If so, what standard is that?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"