CFURLCreateStringByAddingPercent

2017-08-18  本文已影响0人  cl9000

CFURLCreateStringByAddingPercentEscapes() 编码
参考链接:https://github.com/tweetdeck-archive/TDOAuth/issues/16

CF_EXPORT
CFStringRef CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef
 allocator, CFStringRef originalString, CFStringRef charactersToLeaveUnescaped, 
CFStringRef legalURLCharactersToBeEscaped, CFStringEncoding 
encoding) CF_DEPRECATED(10_0, 10_11, 2_0, 9_0, 
"Use [NSString stringByAddingPercentEncodingWithAllowedCharacters:] 
instead, which always uses the recommended UTF-8 encoding, 
and which encodes for a specific URL component or 
subcomponent (since each URL component or subcomponent has different rules for what characters are valid).");

CFURLCreateStringByReplacingPercentEscapesUsingEncoding() 解码
参考链接:https://github.com/futuretap/InAppSettingsKit/issues/359

CF_EXPORT
CFStringRef CFURLCreateStringByReplacingPercentEscapesUsingEncoding
(CFAllocatorRef allocator, CFStringRef origString,
 CFStringRef charsToLeaveEscaped, CFStringEncoding encoding)
 CF_DEPRECATED(10_0, 10_11, 2_0, 9_0, "
Use [NSString stringByRemovingPercentEncoding] or 
CFURLCreateStringByReplacingPercentEscapes() instead, 
which always uses the recommended UTF-8 encoding.");

deprecated in iOS 9.0,将会被弃用

新替换使用方式,规则自己按照自己的来。
url字符串中具有特殊功能的特殊字符的字符串,或者中文字符,作为参数传递时,需要用encode处理一下
对有特殊符号的URL做处理(将特殊字符进行转码)

//编码
+(NSString*)encodeString:(NSString*)uncodeString
{
//    return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) uncodeString,NULL,(CFStringRef) @"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8));
    
    return (NSString *)CFBridgingRelease((__bridge CFTypeRef _Nullable)([[uncodeString description] stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]"] invertedSet]]));
}
//解码
+(NSString*)decodeString:(NSString*)decodeString
{
//    return (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,(__bridge CFStringRef)decodeString,CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
   
    return (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, (CFStringRef)decodeString, CFSTR("")));
}
上一篇 下一篇

猜你喜欢

热点阅读