WKWebView调用window.open(url,"_blank”);没有反应的问题

在使用WKWebView的时候,网页调用window.open(url,"_blank”);发现没有任何响应,也没有调用任何回调,即便写了createWebViewWithConfiguration方法也没用,最终找到原因是没有设置:

preferences.javaScriptCanOpenWindowsAutomatically = YES;

最终解决的代码:

初始化代码:


WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.processPool = processPool;
    WKPreferences *preferences = [WKPreferences new];
    preferences.javaScriptCanOpenWindowsAutomatically = YES;//很重要,如果没有设置这个则不会回调createWebViewWithConfiguration方法,也不会回应window.open()方法
    config.preferences = preferences;
    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    //    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    self.webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.contentMode = UIViewContentModeRedraw;
    self.webView.opaque = YES;
    [self.view addSubview:self.webView];
    [_webView setUserInteractionEnabled:YES];//是否支持交互
    _webView.navigationDelegate = self;
    _webView.UIDelegate = self;//很重要,如果没有设置这个则不会回调UIDelegate相关所有方法

createWebViewWithConfiguration方法代码:在本Webview打开这个网址

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

参考:http://blog.csdn.net/u011619283/article/details/52135982



你可能感兴趣的:(ios)