NSUrl-----------特殊字符的处理

iOS 9之前使用的stringByAddingPercentEscapesUsingEncoding(只对 `#%^{}[]|\"<> 加空格共14个字符编码,不包括”&?”等符号), ios9将淘汰,建议用stringByAddingPercentEncodingWithAllowedCharacters方法

ios10之后开始使用stringByAddingPercentEncodingWithAllowedCharacters 需要传入一个NSCharacterSet对象

(1)常用的有

URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}

URLHostAllowedCharacterSet      "#%/<>?@\^`{|}

URLPasswordAllowedCharacterSet  "#%/:<>?@[\]^`{|}

URLPathAllowedCharacterSet      "#%;<>?[\]^`{|}

URLQueryAllowedCharacterSet    "#%<>[\]^`{|}

URLUserAllowedCharacterSet      "#%/:<>?@[\]^`


(2)网络链接包含有中文字符的时候URL链接可以这样写

NSString *str= [_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容