https://blog.csdn.net/huanghuipost/article/details/102521074
https://www.cnblogs.com/thinksasa/p/3530813.html
https://www.cnblogs.com/huzi007/p/4174519.html
https://blog.csdn.net/weixin_33901926/article/details/89039096
第一:
URL中含有轉義字符例如 雙引號 "" 、大括號{}等
用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
作者:Kingsleeeey
鏈接:https://www.jianshu.com/p/dbb15f019127
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。