设为首页 加入收藏

TOP

iOS url出现特殊字符处理 -- stringByAddingPercentEncodingWithAllowedCharacters
2019-08-26 06:55:13 】 浏览:23
Tags:iOS url 出现 特殊 字符 处理 stringByAddingPercentEncodingWithAllowedCharacters
stringByAddingPercentEscapesUsingEncoding(只对 `#%^{}[]|\"<> 加空格共14个字符编码,不包括”&?”等符号), ios9将淘汰,建议用 stringByAddingPercentEncodingWithAllowedCharacters 方法

stringByAddingPercentEncodingWithAllowedCharacters  需要传一个  NSCharacterSet  对象(关于  NSCharacterSet 这篇 文章说的很好)

如[NSCharacterSet  URLQueryAllowedCharacterSet]

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

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

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

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

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

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

(2)网络访问请求:中文空格字符解码

stringByRemovingPercentEncoding  ----  xcode7可能会提示要将stringByAddingPercentEscapesUsingEncoding替换成此方法,要根据是否是解码来区分

代替  stringByAddingPercentEscapesUsingEncoding

let customAllowedSet =  NSCharacterSet(charactersInString:"`#%^{}\"[]|\\<> ").invertedSet

NSString *urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "] invertedSet]];

 

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS 倒计时的一种实现 下一篇ios开发网络篇—Get请求和Post请..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目