[iOS]stringByAddingPercentEscapesUsingEncoding vs stringByAddingPercentEncodingWithAllowedCharacters

'stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.

如上述文字,不要再用過期的stringByAddingPercentEscapesUsingEncoding,請愛用stringByAddingPercentEncodingWithAllowedCharacters

但stringByAddingPercentEncodingWithAllowedCharacters還要指定你要用哪一款Character Sets,像我的網址如下
http://myhost/User/karen_chang@quantatw.com.png?ModifyDate=2018-06-14T15:30:33.819000
我不希望query string後面的string被解譯錯誤,那就要選用URLQueryAllowedCharacterSet

其他五款的選用時機,請參考Apple Document
不要看網路上都用URLHostAllowedCharacterSet就亂抄,請實際依你的網址選用適合的

沒有留言: