iOS 9之后蘋果推薦使用編碼方法
[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]]
- 其中對(duì)NSCharacter?Set類就很值得研究
- 此處用到URLPathAllowedCharacterSet并不在 NSCharacterSet.h類文件中
// URLFragmentAllowedCharacterSet "#%<>^`\[]{|}
// URLPasswordAllowedCharacterSet "#%<>^`\[]{|}/:?@
// URLPathAllowedCharacterSet "#%<>^`\[]{|};?
// URLQueryAllowedCharacterSet "#%<>^`\[]{|}
// URLUserAllowedCharacterSet "#%<>^`\[]/:?@
// URLHostAllowedCharacterSet "#%<>^`\{|}/?@
- 對(duì)于中文、中文標(biāo)點(diǎn)符號(hào)以及空格都是要編碼的
- 每個(gè)屬性后面對(duì)應(yīng)的特殊符號(hào)也都是要編碼的
- emoji表情也是都要編碼的
- 對(duì)于字母、數(shù)字英文標(biāo)點(diǎn)符號(hào)+-/* 都直接顯示不編碼
- 其中對(duì)應(yīng)后面沒有提到的特殊字符大都不編碼
NSString *string = @"$&1111??aaaaa#";
NSString *unicode = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];

屏幕快照 2019-03-29 16.25.53.png
打印結(jié)果:$&1111%F0%9F%98%86aaaaa%23
由此可以看出表情?? 和 # 是進(jìn)行編碼 的其他字符是不進(jìn)行編碼。