Rami Ammoun
Rami Ammoun

Reputation: 857

Sharing on WhatsApp in Swift 3.0 Error: Contextual member 'urlHostAllowed' has no associated value

I just upgraded my swift project to swift 3.

I've been using the following function to share the app on Whatsapp, but I couldn't understand the error that occurred after the upgrade

This is the function code:

func shareOnWhatsapp() {
    let urlString = "Greetings,\n\nThis is the XYZ App link, I hope you find it useful!\n\nhttp://itunes.apple.com/app/idxxxxxxxx"
    let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed())
    let url  = URL(string: "whatsapp://send?text=\(urlStringEncoded!)")

    if UIApplication.shared.canOpenURL(url!) {
        UIApplication.shared.openURL(url!)
    }

}

The error says:

Contextual member 'urlHostAllowed' has no associated value

Any idea how to resolve this?

Upvotes: 2

Views: 1236

Answers (1)

real 19
real 19

Reputation: 748

You need to get rid of the "()" in the third line:

let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed())

to this:

let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

Upvotes: 5

Related Questions