Skip to content

Instantly share code, notes, and snippets.

@Wooder
Created August 1, 2017 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wooder/f08a1d40fe345806c36fc0faf36cce1a to your computer and use it in GitHub Desktop.
Save Wooder/f08a1d40fe345806c36fc0faf36cce1a to your computer and use it in GitHub Desktop.
private func urlEncodeString(_ string: String) -> String {
#if swift(>=3.0)
let allowedCharacterSet = (CharacterSet(charactersIn: "!*'();:@&=+$,/?%#[] ").inverted)
if let escapedString = string.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) {
return escapedString
}
#else
let allowedCharacterSet = (NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[] ").invertedSet)
if let escapedString = string.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacterSet) {
return escapedString
}
#endif
Log.Error.log("Could not urlencode \(string)")
return string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment