在Swift 3.0中分享WhatsApp错误:上下文成员&urlHostAllowed'没有相关价值

时间:2016-09-18 03:11:18

标签: swift swift3

我刚刚将我的swift项目升级为swift 3.

我一直在使用以下功能在Whatsapp上分享应用,但我无法理解升级后发生的错误

这是功能代码:

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!)
    }

}

错误说:

Contextual member 'urlHostAllowed' has no associated value

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:5)

你需要摆脱"()"在第三行:

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

到此:

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