encoding url using swift code

inanva picture inanva · Apr 1, 2015 · Viewed 21k times · Source

I need to send an URL in Arabic language, so I need to encode it before I put it in URL. I am using Swift code.

Below is an example what i really need

var s = "www.example.com/السلام عليكم"

let url = NSURL(string : s)

So the word (السلام عليكم) is in Arabic characters that what I want to send.

Answer

yarlg picture yarlg · Jul 2, 2016

Swift 2.0

let urlwithPercentEscapes = myurlstring.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())

Swift 3

let urlwithPercentEscapes = myurlstring.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)