Ios Swift 3开放链接

Ios Swift 3开放链接,ios,xcode,hyperlink,swift3,Ios,Xcode,Hyperlink,Swift3,我尝试在点击按钮时运行此功能: @IBAction func openLink(_ sender: UIButton) { let link1 = "https://www.google.com/#q=" let link2 = birdName.text! let link3 = link2.replacingOccurrences(of: " ", with: "+") //EDIT let link4 = link1+link3 guard

我尝试在点击按钮时运行此功能:

@IBAction func openLink(_ sender: UIButton) {
    let link1 = "https://www.google.com/#q="
    let link2 = birdName.text!
    let link3 = link2.replacingOccurrences(of: " ", with: "+") //EDIT
    let link4 = link1+link3
    guard
        let query = link4.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed),
        let url = NSURL(string: "https://google.com/#q=\(query)")
        else { return }
    UIApplication.shared.openURL(URL(url))
}

但是,最后一行被标记为不能调用非函数类型UIApplication的值。此语法来自此处,因此我不确定发生了什么。

使用guard打开textfield text属性,替换出现的内容,将百分比编码添加到结果中,并根据结果字符串创建URL:

试着这样做:

guard
    let text = birdName.text?.replacingOccurrences(of: " ", with: "+"),
    let query = text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
    let url = URL(string: "https://google.com/#q=" + query)
else { return }
if #available(iOS 10.0, *) {
    UIApplication.shared.open(url)
} else {
    UIApplication.shared.openURL(url)
}

使用guard展开textfield text属性,替换出现的内容,将百分比编码添加到结果中,并根据结果字符串创建URL:

试着这样做:

guard
    let text = birdName.text?.replacingOccurrences(of: " ", with: "+"),
    let query = text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
    let url = URL(string: "https://google.com/#q=" + query)
else { return }
if #available(iOS 10.0, *) {
    UIApplication.shared.open(url)
} else {
    UIApplication.shared.openURL(url)
}

这会导致应用程序在按下按钮时崩溃。在展开可选值时意外发现为零。请确保使用了正确的url链接。你可能不得不转义你的查询字符串。我在浏览器上尝试了这个链接,链接很好。什么是逃逸百分比?这就解决了它!非常感谢。这会导致应用程序在按下按钮时崩溃。在展开可选值时意外发现为零。请确保使用了正确的url链接。你可能不得不转义你的查询字符串。我在浏览器上尝试了这个链接,链接很好。什么是逃逸百分比?这就解决了它!非常感谢。我真的很感激像你这样的人帮助像我们这样的人解决我们没有那么受过教育的错误和问题。你真的在帮助我们谋生!我真的很感激像你这样的人帮助像我们这样的人解决我们没有那么受过教育的错误和问题。你真的在帮助我们谋生!