简体   繁体   中英

Open WebView Links on SFSafariViewController

The app opens articles in UIWebView, and i need the links of those articles to open on SFSafariViewController (it's mandatory). So far it will open on the webview and mess up my user experience. My WebView loads HTML Strings and load the content, like below.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.webView.loadHTMLString(article.content, baseURL: nil)
}

With the content, links will come, like the image below. screenshot of the app

How can I make those links of my articles open on SFSafariViewController? What should I do since it comes from HTML Strings and i don't know the article links url?

That worked. Create a Bool variable called allowLoad to allow or not the webview content to load. After you load the article on the webview the allowLoad will become false, so the next requests will only be able to begin on link touchs, then it will show the SFSafariViewController (i dont know if you undersant, so you can ask me anything)

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    if (navigationType == UIWebViewNavigationType.LinkClicked){
        let url = request.URL;

        if #available(iOS 9.0, *) {
            let sfViewController = SFSafariViewController(URL:url!, entersReaderIfAvailable: true)

            self.presentViewController(sfViewController, animated: true, completion: { () -> Void in
                print("May the force be with you!")
            })
        } else {

        }
    }

    return allowLoad
}

func webViewDidFinishLoad(webView: UIWebView) {
    return allowLoad = false
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM