URL轉(zhuǎn)譯 stringByAddingPercentEscapesUsingEncoding(只對(duì) `#%^{}[]|"<> 加空格共14個(gè)字符編碼,不包括”&?”等符號(hào)), ios9將淘汰消痛。
建議用stringByAddingPercentEncodingWithAllowedCharacters方法
//中文鏈接
NSString *globalURL = [[NSString alloc]initWithFormat:@"http://127.0.0.1:%d/oa/workflow/submit/workflow?requestId=181193&userNumber=01281209&type=submit&remark=這是個(gè)什么鬼啊佣蓉,這是個(gè)神",ListenPort];
//iOS9.0廢棄方法
//NSString * encodingString = [globalURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//轉(zhuǎn)譯后的鏈接
NSString * encodingString = [globalURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
stringByAddingPercentEncodingWithAllowedCharacters需要傳一個(gè)
NSCharacterSet對(duì)象(關(guān)于 NSCharacterSet 這篇 文章說的很好)
如[NSCharacterSet URLQueryAllowedCharacterSet]
@interface NSCharacterSet (NSURLUtilities)
// 預(yù)定義字符集用于六個(gè)URL組件和子組件迅脐,它們?cè)试S百分比編碼欣鳖。
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
// 包含URL用戶子組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLUserAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
// 包含URL密碼子組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLPasswordAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
// 包含URL的主機(jī)子組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLHostAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
// 返回一個(gè)包含字符的字符集允許URL的路徑組件漫蛔。字符“;”是一種合法的路徑,但是建議最好是percent-encoded兼容NSURL(-stringByAddingPercentEncodingWithAllowedCharacters:percent-encode任何‘;’字符如果你通過URLPathAllowedCharacterSet)
@property (class, readonly, copy) NSCharacterSet *URLPathAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
// 包含URL查詢組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLQueryAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
// 包含URL片段組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLFragmentAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
@end
編碼字符范圍
URLFragmentAllowedCharacterSet "#%<>[\]^`{|}
URLHostAllowedCharacterSet "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet "#%<>[\]^`{|}
URLUserAllowedCharacterSet "#%/:<>?@[\]^`
END