SFSafariViewController crash: The specified URL has an unsupported scheme

后端 未结 4 1908
渐次进展
渐次进展 2021-02-07 06:23

My code:

if let url = NSURL(string: \"www.google.com\") {
    let safariViewController = SFSafariViewController(URL: url)
    safariViewController.view.tintColor         


        
4条回答
  •  一整个雨季
    2021-02-07 07:22

    Try checking scheme of URL before making an instance of SFSafariViewController.

    Swift 3:

    func openURL(_ urlString: String) {
        guard let url = URL(string: urlString) else {
            // not a valid URL
            return
        }
    
        if ["http", "https"].contains(url.scheme?.lowercased() ?? "") {
            // Can open with SFSafariViewController
            let safariViewController = SFSafariViewController(url: url)
            self.present(safariViewController, animated: true, completion: nil)
        } else {
            // Scheme is not supported or no scheme is given, use openURL
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }
    

    Swift 2:

    func openURL(urlString: String) {
        guard let url = NSURL(string: urlString) else {
            // not a valid URL
            return
        }
    
        if ["http", "https"].contains(url.scheme.lowercaseString) {
            // Can open with SFSafariViewController
            let safariViewController = SFSafariViewController(URL: url)
            presentViewController(safariViewController, animated: true, completion: nil)
        } else {
            // Scheme is not supported or no scheme is given, use openURL
            UIApplication.sharedApplication().openURL(url)
        }
    }
    

提交回复
热议问题