i am using a GET method in which i have to pass a email address in the URL. API expects it to be encoded. I tried with the encoding options but the \'+\' character couldnt b
Unfortunately, both CharacterSet.urlHostAllowed
and CharacterSet.urlQueryAllowed
contains +
as allowed. And for historical reason, most web servers treat +
as a replacement of whitespace (), so you need to escape
+
.
For such purpose, you may need to define your own CharacterSet
:
extension CharacterSet {
static let rfc3986Unreserved = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~")
}
let emailAddressText = "mano+1@gmail.com"
let encodedEmail = emailAddressText.addingPercentEncoding(withAllowedCharacters:.rfc3986Unreserved)
print(encodedEmail!) //->mano%2B1%40gmail.com