How do I URL encode a string

前端 未结 22 1963
轮回少年
轮回少年 2020-11-22 07:16

I have a URL string (NSString) with spaces and & characters. How do I url encode the entire string (including the & ampersand

22条回答
  •  深忆病人
    2020-11-22 08:05

    So many answers but didn't work for me, so I tried the following:

    fun simpleServiceCall(for serviceUrl: String, appendToUrl: String) { 
        let urlString: String = serviceUrl + appendToUrl.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!     
    
        let finalUrl = URL(string: urlString)!
    
        //continue to execute your service call... 
    }
    

    Hopefully it'll help someone. thanks

提交回复
热议问题