encoding url using swift code

后端 未结 6 1192
野性不改
野性不改 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:43

    To improve @Druva's answer create an extention somewhere in the project

    Swift 2.0

    extension String
    {   
        func encodeUrl() -> String
        {
            return self.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())
        }
    func decodeUrl() -> String
        {
            return self.stringByRemovingPercentEncoding
        }
    
    }
    

    Swift 3.0

     extension String
        {   
            func encodeUrl() -> String
            {
                return self.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
            }
        func decodeUrl() -> String
            {
                return self.stringByRemovingPercentEncoding
            }
    
        }
    

提交回复
热议问题