简体   繁体   English

在 SFSafariViewController 上打开 WebView 链接

[英]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).该应用程序在 UIWebView 中打开文章,我需要在 SFSafariViewController 上打开这些文章的链接(这是强制性的)。 So far it will open on the webview and mess up my user experience.到目前为止,它将在 webview 上打开并弄乱我的用户体验。 My WebView loads HTML Strings and load the content, like below.我的 WebView 加载 HTML 字符串并加载内容,如下所示。

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?如何在 SFSafariViewController 上打开我文章的这些链接? What should I do since it comes from HTML Strings and i don't know the article links url?因为它来自 HTML 字符串并且我不知道文章链接 url,我该怎么办?

That worked.那奏效了。 Create a Bool variable called allowLoad to allow or not the webview content to load.创建一个名为 allowLoad 的 Bool 变量以允许或不允许加载 webview 内容。 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)在 webview 上加载文章后,allowLoad 将变为 false,因此下一个请求将只能在链接触摸时开始,然后它将显示 SFSafariViewController(我不知道你是否理解,所以你可以问我任何问题)

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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