Safari中的Swift Open Link

时间:2014-09-20 04:44:09

标签: webview swift safari

我目前正在WebView打开我的应用中的链接,但我正在寻找一个选项来打开 Safari 中的链接。

10 个答案:

答案 0 :(得分:260)

它没有"烘焙到Swift",但您可以使用标准的UIKit方法来完成它。看看UIApplication's openUrl()

Swift 4

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.open(url)

Swift 3

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.openURL(url)

Swift 2.2

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.sharedApplication().openURL(url)    

答案 1 :(得分:50)

iOS 9及更高版本的新功能,您可以向用户显示SFSafariViewController(请参阅文档here)。基本上,您可以获得将用户发送到Safari而不会让他们离开您的应用程序的所有好处。要使用新的SFSafariViewController:

import SafariServices

以及事件处理程序中的某个位置向用户显示safari视图控制器,如下所示:

let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)

safari视图将如下所示:

enter image description here

答案 2 :(得分:18)

更新了Swift 4 :(归功于Marco Weber)

if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
     UIApplication.shared.openURL(requestUrl as URL) 
}

或者使用guard更多的快捷风格:

guard let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") else {
    return
}

UIApplication.shared.openURL(requestUrl as URL) 

斯威夫特3:

您可以通过以下方式隐式检查NSURL是否为可选项:

if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
     UIApplication.sharedApplication().openURL(requestUrl)
}

答案 3 :(得分:11)

Swift 3& IOS 10.2

UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
  斯威夫特3& IOS 10.2

答案 4 :(得分:9)

因为iOS 10你应该使用:

guard let url = URL(string: linkUrlString) else {
        return
    }

    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }

答案 5 :(得分:4)

快速5

Swift 5: Check using canOpneURL if valid then it's open.

guard let url = URL(string: "https://iosdevcenters.blogspot.com/") else {
     return
 }
if UIApplication.shared.canOpenURL(url) {
     UIApplication.shared.open(url, options: [:], completionHandler: nil)
 }

答案 6 :(得分:2)

在Swift 1.2中:

{{1}}

答案 7 :(得分:1)

在Swift 2.0中:

UIApplication.sharedApplication().openURL(NSURL(string: "http://stackoverflow.com")!)

答案 8 :(得分:0)

雨燕5

if let url = URL(string: "https://www.google.com") {
    UIApplication.shared.open(url)
}

答案 9 :(得分:-1)

IOS 11.2 Swift 3.1-4

let webView = WKWebView()
override func viewDidLoad() {
    super.viewDidLoad()
    guard let url = URL(string: "https://www.google.com") else { return }
    webView.frame = view.bounds
    webView.navigationDelegate = self
    webView.load(URLRequest(url: url))
    webView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
    view.addSubview(webView)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if navigationAction.navigationType == .linkActivated  {
        if let url = navigationAction.request.url,
            let host = url.host, !host.hasPrefix("www.google.com"),
            UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url)
            print(url)
            print("Redirected to browser. No need to open it locally")
            decisionHandler(.cancel)
        } else {
            print("Open it locally")
            decisionHandler(.allow)
        }
    } else {
        print("not a user click")
        decisionHandler(.allow)
    }
}

}