SFSafariViewController crash: The specified URL has an unsupported scheme

后端 未结 4 1957
渐次进展
渐次进展 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:03

    I did a combination of Yuvrajsinh's & hoseokchoi's answers.

    func openLinkInSafari(withURLString link: String) {
    
        guard var url = NSURL(string: link) else {
            print("INVALID URL")
            return
        }
    
        /// Test for valid scheme & append "http" if needed
        if !(["http", "https"].contains(url.scheme.lowercaseString)) {
            let appendedLink = "http://".stringByAppendingString(link)
    
            url = NSURL(string: appendedLink)!
        }
    
        let safariViewController = SFSafariViewController(URL: url)
        presentViewController(safariViewController, animated: true, completion: nil)
    }
    

提交回复
热议问题