special characters not being encoded properly inside URLQueryItems

1.7k views Asked by At

I need to create firebase deep links in my app to share them on different platforms using UIActivityViewController . In order to create theses links, Im using the following code with URLQueryItem to get the url I will share to others apps :

func createURLWithComponents(EventToUrl: Event) -> URL? {

    var urlComponents = URLComponents()
    urlComponents.scheme = "https"
    urlComponents.host = "www.example.com"
    urlComponents.path = "/"

    // add params
    let id = URLQueryItem(name: "id", value: EventToUrl.eventID)

    print(EventToUrl.name)
    let name = URLQueryItem(name: "name", value: EventToUrl.name)
    print(name.description)
    print(name.value)
    let photo = URLQueryItem(name: "photo", value:  EventToUrl.photoUrl)
    let created = URLQueryItem(name: "created", value: EventToUrl.creationDate )
    let participants = URLQueryItem(name: "participants", value: String(EventToUrl.Participants.count) )
    let place = URLQueryItem(name: "place", value: EventToUrl.Places[0].name)
    let time = URLQueryItem(name: "time", value: EventToUrl.time[0].TimestampString)
    urlComponents.queryItems = [id, name, photo, created, participants , place , time]

    let dynamicLinkDomain : String = "***.app.goo.gl"
    var deepLink = URLComponents()
    deepLink.scheme = "https"
    deepLink.host = dynamicLinkDomain
    deepLink.path = "/"

    let link = URLQueryItem(name: "link", value:  urlComponents.url?.absoluteString)
    let apn = URLQueryItem(name: "apn", value: "***" )
    let ibi = URLQueryItem(name: "ibi", value: "***" )
    let d = URLQueryItem(name: "d", value: "1")
    deepLink.queryItems = [link, apn, ibi, d]
    print(deepLink.url?.absoluteString)
    print(deepLink.url)

    return deepLink.url
}

Here is my link when I send him :

https://***.app.goo.gl/?link=https://www.example.com/?id%3D-KZYfNRCXdSqUG72FmEQ%26name%3Djeremie's%2520Event%26photo%3D%26created%3D%26participants%3D1%26place%3DLa%2520Piazza%26time%3D1482362284.50304&apn=***&ibi=***&d=1

https://.app.goo.gl/?link=https://www.example.com/?id%3D-KZYfNRCXdSqUG72FmEQ%26name%3Djeremie's%2520Event%26photo%3D%26created%3D%26participants%3D1%26place%3DLa%2520Piazza%26time%3D1482362284.50304&apn=&ibi=***&d=1

As you can see ' if the event name is composed with " ' " character (Jeremie's event) , the URLQueryItem doesn't properly encode this character (in my android app , system function transform it to "'" --> %27) How Can I encode it? PS : I try with addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) without sucess... Thanks for your help!

1

There are 1 answers

0
R.P. Carson On

I found a solution that is a little simpler than the others. (it is to me at least)

I constructed my URLComponents as normal, but before getting the url from it, I took the urlComponents.string and replaced the special codes with the symbols. I run that string through addingPercentEncoding and then make a url from this string.

let stringWithoutBS = urlComponents.string!.replacingOccurrences(of: "%27", with: "'")
        //ignore the force unwrap

guard let encodedURL = stringWithoutBS.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {
            print("unhelpful message")
            return nil
        }

var correctURL = URL(string: encodedURL)