iOS WebView:在外部浏览器中打开单个超链接



我的iOS应用中有一个简单的UIWebView。超链接在我的应用程序本身中正常打开。但是我想在外标准浏览器(Safari)中打开一个超链接。

i映像一些like <a href="http://example.com" target="_safari">

有这样的东西吗?

我无法在应用程序本身中进行更改。我必须在我的网站上这样做!

您可以使用UIWebViewDelegate方法来实现这一目标。实施以下代表的方法

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    let urlStr = request.url?.absoluteString
    if //urlStr is string you want {
         //launch external browser
         return false
    }
    return true
}

每次您尝试在Web视图中加载URL时,此方法都会被调用。您可以比较是否要加载其URL,如果匹配它,则可以启动外部浏览器并返回false。

尝试这种方式

<a onclick="window.open('https://www.google.co.in/', '_system');" href="#">Google</a>

最新更新