遇到一个问题,webview加载一段URL显示不了,URL是正确的,后来发现url路劲里有个#号,打印的时候被转义了,导致页面显示不出来,特意去网上找了下资料,记录下:
stringByAddingPercentEscapesUsingEncoding
该方法只对 `#%^{}[]|\"<> 加空格共14个字符编码,不包括”&?”等符号,
iOS9将淘汰,建议用stringByAddingPercentEncodingWithAllowedCharacters方法
stringByAddingPercentEncodingWithAllowedCharacters方法需要传一个参数NSCharacterSet对象。
关于 NSCharacterSet 这篇 文章说的很好
NSCharacterSet举例说明:
URLFragmentAllowedCharacterSet "#%<>[\]^`{|}
URLHostAllowedCharacterSet "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet "#%<>[\]^`{|}
URLUserAllowedCharacterSet "#%/:<>?@[\]^`
当URL里有特殊字符不需要转义时,处理方法:
NSString *unreserved = @"-._~/?#:";
NSMutableCharacterSet *allowed = [NSMutableCharacterSet alphanumericCharacterSet];
[allowed addCharactersInString:unreserved];
NSString *result = [string stringByAddingPercentEncodingWithAllowedCharacters:allowed];