iOS中如何对URL的param强制encode,不影响前面的http...等

如何encode:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let urlStrEncode = urlStr.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let urlStrEncode = urlStr.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let urlStrEncode = urlStr.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

或者

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let urlStrEncode = urlStr.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())
let urlStrEncode = urlStr.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())
let urlStrEncode = urlStr.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())

顺道再贴下如何decode:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let urlStrDecode = urlStrEncoded.stringByRemovingPercentEncoding
let urlStrDecode = urlStrEncoded.stringByRemovingPercentEncoding
let urlStrDecode = urlStrEncoded.stringByRemovingPercentEncoding

Leave a Reply

Your email address will not be published. Required fields are marked *