用stringByAddingPercentEncodingWithAllowedCharacters取代CFURLCreateStringByAddingPercentEscapes
1.网络访问请求:中文空格字符编码/解码
在 iOS 程序访问 HTTP 资源时需要对 URL 进行 Encode,
比如像拼出来的 http://unmi.cc?p1=%+&sd f&p2=中文,
其中的中文、特殊符号&%和空格都必须进行转译才能正确访问。
现在以“?!@#$^&%*+,:;='\”`<>()[]{}/\| "字符串为例子,
用stringByAddingPercentEncodingWithAllowedCharacters取代
CFURLCreateStringByAddingPercentEscapes
stringByAddingPercentEscapesUsingEncoding
(只对 `#%^{}[]|"<> 加空格共14个字符编码,不包括“&?”等符号),
iOS 9将淘汰,建议用stringByAddingPercentEncodingWithAllowedCharacters方法
URLUserAllowedCharacterSet "#%/:<>?@[\]^`
URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|}
URLHostAllowedCharacterSet "#%/<>?@\^`{|}
URLPathAllowedCharacterSet "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet "#%<>[\]^`{|}
URLFragmentAllowedCharacterSet "#%<>[\]^`{|}
2.网络访问请求:中文空格字符解码
stringByRemovingPercentEncoding :
Xcode7可能会提示要将stringByAddingPercentEscapesUsingEncoding替换成此方法,要根据是否是解码来区分
//代替stringByAddingPercentEscapesUsingEncoding
let customAllowedSet = NSCharacterSet(charactersInString:"`#%^{}\"[]|\\<> ").invertedSet
NSString *resourcePath = @"http://www.jianshu.com/users/ac47ba96ae2f/latest_articles";
NSString *encodePath ;
if (!IOS7_OR_LATER) {
encodePath = [resourcePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
} else {
encodePath = [resourcePath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
}
举例:
原字符串为:
NSString *url = @"ertehtt""p://xxdsdscrg?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
CFURLCreateStringByAddingPercentEscapes方法为:
CFStringRef encodedCFString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef) url, nil, CFSTR("?!@#$^&%*+,:;='\"`<>()[]{}/\\| "), kCFStringEncodingUTF8);
NSString *encodedString = [[NSString alloc] initWithString:(__bridge_transfer NSString*) encodedCFString];
stringByAddingPercentEncodingWithAllowedCharacters方法为:
NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
NSString *encodedUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
NSLog(@"\n%@\n%@",encodedUrl,encodedString);
之后获得的字符串为一致的。
问题实例:
我有一个有效的 url,我得到 不受支持的 url 错误。正如你可以看到 http://
//http://www.jianshu.com/users/ac47ba96ae2f/latest_articles
Error description=Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x78f97920 {NSUnderlyingError=0x79f78bd0 "unsupported URL", NSLocalizedDescription=unsupported URL}
这是我想试图如何初始化 url:
方法 1:
NSString *path = @"http://www.jianshu.com/users/ac47ba96ae2f/latest_articles";
NSURL *url = [NSURL URLWithString:path];
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];
方法 2:
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.jianshu.com/users/ac47ba96ae2f/latest_articles"]];
解决方法 :
URL 不能包含 ASCII 字符集中, 不是必须这样的字符进行转义的字符。
使用 stringByAddingPercentEncodingWithAllowedCharacters
字符集 URLQueryAllowedCharacterSet
NSString *path = @"http://www.jianshu.com/users/ac47ba96ae2f/latest_articles";
NSString *escapedPath = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@"escapedPath: %@", escapedPath);
输出:
escapedPath: http://www.jianshu.com/users/ac47ba96ae2f/latest_articles