用stringByAddingPercentEncodingWithAllowedCharacters取代CFURLCreateStringByAddingPercentEscapes
1.網(wǎng)絡(luò)訪問(wèn)請(qǐng)求:中文空格字符編碼/解碼
在 iOS 程序訪問(wèn) HTTP 資源時(shí)需要對(duì) URL 進(jìn)行 Encode,
比如像拼出來(lái)的 http://unmi.cc?p1=%+&sd f&p2=中文,
其中的中文、特殊符號(hào)&%和空格都必須進(jìn)行轉(zhuǎn)譯才能正確訪問(wèn)。
現(xiàn)在以“?!@#$^&%*+,:;='\”`<>()[]{}/\| "字符串為例子,
用stringByAddingPercentEncodingWithAllowedCharacters取代
CFURLCreateStringByAddingPercentEscapes
stringByAddingPercentEscapesUsingEncoding
(只對(duì) `#%^{}[]|"<> 加空格共14個(gè)字符編碼,不包括“&?”等符號(hào)),
iOS 9將淘汰,建議用stringByAddingPercentEncodingWithAllowedCharacters方法
URLUserAllowedCharacterSet "#%/:<>?@[\]^`
URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|}
URLHostAllowedCharacterSet "#%/<>?@\^`{|}
URLPathAllowedCharacterSet "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet "#%<>[\]^`{|}
URLFragmentAllowedCharacterSet "#%<>[\]^`{|}
2.網(wǎng)絡(luò)訪問(wèn)請(qǐng)求:中文空格字符解碼
stringByRemovingPercentEncoding :
Xcode7可能會(huì)提示要將stringByAddingPercentEscapesUsingEncoding替換成此方法,要根據(jù)是否是解碼來(lái)區(qū)分
//代替stringByAddingPercentEscapesUsingEncoding
let customAllowedSet = NSCharacterSet(charactersInString:"`#%^{}\"[]|\\<> ").invertedSet
NSString *resourcePath = @"http://www.itdecent.cn/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);
之后獲得的字符串為一致的。
問(wèn)題實(shí)例:
我有一個(gè)有效的 url,我得到 不受支持的 url 錯(cuò)誤。正如你可以看到 http://
//http://www.itdecent.cn/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.itdecent.cn/users/ac47ba96ae2f/latest_articles";
NSURL *url = [NSURL URLWithString:path];
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];
方法 2:
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.itdecent.cn/users/ac47ba96ae2f/latest_articles"]];
解決方法 :
URL 不能包含 ASCII 字符集中, 不是必須這樣的字符進(jìn)行轉(zhuǎn)義的字符。
使用 stringByAddingPercentEncodingWithAllowedCharacters
字符集 URLQueryAllowedCharacterSet
NSString *path = @"http://www.itdecent.cn/users/ac47ba96ae2f/latest_articles";
NSString *escapedPath = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@"escapedPath: %@", escapedPath);
輸出:
escapedPath: http://www.itdecent.cn/users/ac47ba96ae2f/latest_articles