Rami Ammoun Rami Ammoun - 2 months ago 98
Swift Question

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?

Answer

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)