WKWebView 如何支持window.open方法

11 篇文章 0 订阅
6 篇文章 0 订阅

window.open是js新开页面的一种方法,本质上是为了在PC上支持直接新开页面,但是移动端也是支持该方法的,只是处理方式上并没有让它扮演新开窗口的任务,通过window.open打开的页面链接其实还是在当前webview内进行加载。iOS平台UIWebView默认支持了这一js命令,但是WKWebView默认是不支持的。

第一、UIWebView下window.open的表现

如前所述,UIWebView下window.open的功能和a标签类似,但也有区别:

其共同之处在于:二者都可以响应点击事件,触发一个链接加载;

其不同之处在于:a标签需要一个完整的加载url链接;但是window.open有两种情况:

window.open的最基本的调用方式为:

window.open(URL)

这里的参数URL不同,则移动端的表现差异很大:

(一)、若URL为一个带有scheme的url,如https://www.baidu.com,则其响应为加载https://www.baidu.com;

(二)、若URL不带有scheme,则其表现是用URL替换掉当前页面的url的path(url的标准组成部分之一),然后加载替换后的url。

举例:

如当前的页面为https://www.baidu.com/index.html, 则其path为/index.html。现在如果在该页面内调用window.open('www.jd.com'),则最终加载的url为https://www.baidu.com/www.jd.com,所以,window.open在移动端调用的时候必须小心。如果是想打开新的链接,必须带有scheme。

第二、wkwebview如何支持?

前面说过wkwebview默认是不支持这个方法的,需要进行设置。网上有些误人的做法,即在

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{

        if(navigationAction.targetFrame == nil || || !navigationAction.targetFrame.isMainFrame)
        {
            [webView loadRequest:navigationAction.request];
        }
        decisionHandler(WKNavigationActionPolicyAllow);
}

因为在window.open触发加载的情况下navigationAction.targetFrame == nil,所以期望通过这种方式解决,但这段代码放在此回调中是没有任何作用的。实际上window.open在默认情况下并不会触发上述回调。

我们首先从wkwebview的属性设置上来进行了尝试,发现有个属性:

/*! @abstract A Boolean value indicating whether JavaScript can open
 windows without user interaction.
 @discussion The default value is NO in iOS and YES in OS X.
 */
@property (nonatomic) BOOL javaScriptCanOpenWindowsAutomatically;

从方法的说明上看是最有可能解决问题的方法。我们将此属性设置为YES。But......

未解决问题!!!!!!!!!!

后来我们对WKWebview的代理方法的分析我们有如下发现:

/*! @abstract Creates a new web view.
 @param webView The web view invoking the delegate method.
 @param configuration The configuration to use when creating the new web
 view.
 @param navigationAction The navigation action causing the new web view to
 be created.
 @param windowFeatures Window features requested by the webpage.
 @result A new web view or nil.
 @discussion The web view returned must be created with the specified configuration. WebKit will load the request in the returned web view.

 If you do not implement this method, the web view will cancel the navigation.
 */
- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;

这个WKUIDelegate的代理方法的作用是创建一个新的webview,和window.open的作用类似,我们来实现上述代理方法

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
    if(navigationAction.targetFrame == nil || !navigationAction.targetFrame.isMainFrame)
    {
        [webView loadRequest:navigationAction.request];
    }
    return nil;
}

这里return nil是不开新的webview,而就在当前webview加载,行为上和UIWebview是一致的。

实现这个方法后,问题得到了解决!!!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值