Swift Open Link in Safari

前端 未结 10 2420
北荒
北荒 2020-12-01 00:41

I am currently opening the link in my app in a WebView, but I\'m looking for an option to open the link in Safari instead.

相关标签:
10条回答
  • 2020-12-01 00:51

    since iOS 10 you should use:

    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)
    }
    
    0 讨论(0)
  • 2020-12-01 00:52

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-01 00:54

    New with iOS 9 and higher you can present the user with a SFSafariViewController (see documentation here). Basically you get all the benefits of sending the user to Safari without making them leave your app. To use the new SFSafariViewController just:

    import SafariServices
    

    and somewhere in an event handler present the user with the safari view controller like this:

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

    The safari view will look something like this:

    0 讨论(0)
  • 2020-12-01 00:55

    In Swift 1.2:

    @IBAction func openLink {    
        let pth = "http://www.google.com"
        if let url = NSURL(string: pth){
            UIApplication.sharedApplication().openURL(url)
    }
    
    0 讨论(0)
  • 2020-12-01 00:57

    UPDATED for Swift 4: (credit to Marco Weber)

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

    OR go with more of swift style using guard:

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

    Swift 3:

    You can check NSURL as optional implicitly by:

    if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
         UIApplication.sharedApplication().openURL(requestUrl)
    }
    
    0 讨论(0)
  • 2020-12-01 00:59

    Swift 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)
    }
    
    0 讨论(0)
提交回复
热议问题