禁用WKWebView打开链接以重定向到我的iPhone上安装的应用程序 [英] Disable WKWebView for opening links to redirect to apps installed on my iPhone

查看:530
本文介绍了禁用WKWebView打开链接以重定向到我的iPhone上安装的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我搜索谷歌并点击Etsy.com时,WKWebView会将我重定向到我的iPhone上安装的Etsy应用程序。
如何禁用此行为?我希望WKWebView将我重定向到etsy.com移动网站。
我正在使用swift。

When I'm searching google and click on Etsy.com for exmaple, WKWebView redirect me to Etsy app installed on my iPhone. How can I disable this behavior? I want WKWebView to redirect me to etsy.com mobile website. I'm using swift.

推荐答案

不幸的是, WKWebView 不会将带有自定义方案的网址发送回您的应用以自动处理。

Unfortunately, WKWebView doesn’t send urls with custom schemes back to your app to handle automatically.

如果您在没有特殊处理的情况下尝试此操作,在用户使用第三方服务进行身份验证后您的Web视图将会挂起,并且您永远不会收到回调。您可以尝试使用标准http或https方案的重定向URI,但是 WKWebView 只会尝试加载它,而不是将其从Web视图导出到您的本机要处理的应用程序。

If you try this without special handling, it will look like your web view hangs after the user authenticates with the third-party service and you’ll never receive your callback. You could try using a redirect URI with the standard http or https scheme, but then WKWebView would just try to load it, rather than directing it out of the web view to your native app to handle.

为了处理重定向,你需要在<$ c $中实现 decisionPolicyForNavigationAction c>您的 WKWebView 的WebPolicyDelegate ,以检测自定义 URL 方案并将其指向您要处理的应用:

In order to handle the redirect, you need to implement decidePolicyForNavigationAction in the WebPolicyDelegate of your WKWebView to detect the custom URL scheme and direct it to your app to be handled:

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
        print("webView:\(webView) decidePolicyForNavigationAction:\(navigationAction) decisionHandler:\(decisionHandler)")

        let app = UIApplication.sharedApplication()
        let url = navigationAction.request.URL
        let myScheme: NSString = "https"
        if (url!.scheme == myScheme) && app.canOpenURL(url!) {
            print("redirect detected..")
            // intercepting redirect, do whatever you want
            app.openURL(url!) // open the original url
            decisionHandler(.Cancel)
            return
        }

        decisionHandler(.Allow)
    }

您可以找到详细信息这里

这篇关于禁用WKWebView打开链接以重定向到我的iPhone上安装的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆