encoding url using swift code

后端 未结 6 1209
野性不改
野性不改 2021-02-12 21:03

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

va         


        
6条回答
  •  时光取名叫无心
    2021-02-12 21:56

    You need to encode url as you have written. You can do so with that string method:

    stringByAddingPercentEscapesUsingEncoding(NSStringEncoding)
    

    So your code will be:

    var s = "www.example.com/السلام عليكم"
    // you may add check before force unwrapping
    let url = NSURL(string : s.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
    

提交回复
热议问题