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 be encoded.
i tried with the following code
let encodedEmail = emailAddressTxt.text!.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)
let urlString = "http://www.example.com/User/GetUserDetailsByEmailAddress?EmailAddress=\(encodedEmail!)"
print(escapedString)
It prints http://www.example.com/User/GetUserDetailsByEmailAddress?EmailAddress=mano+1%40gmail.com
Where the '@' character is only encoded and '+' is not encoded.
Unfortunately, both
CharacterSet.urlHostAllowed
andCharacterSet.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
:When you use .urlHostAllowed character set '+' is not encoded.
Add extension to String like below
You can do something like this.
Try this :
or
let encodedEmail = emailAddressTxt.text!