iOS WkWebview不支持 window.open的解决方法

由于iOS的WkWebview对window.open方法进行了安全限制,即调用该方法,不会起到作用。

解放方法:当触发window.open方法时,会触发代理WKUIDelegate中的createWebViewWithConfiguration

- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;

然后,我们就可以在这个方法中进行处理了。需要设置这个代理 webView.UIDelegate = self;

// 当调用window.open方法时,会掉用该代理方法
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
    
    if (navigationAction.request.URL) {
        
        NSURL *url = navigationAction.request.URL;
        NSString *urlPath = url.absoluteString;
        if ([urlPath rangeOfString:@"https://"].location != NSNotFound || [urlPath rangeOfString:@"http://"].location != NSNotFound) {
            
            [[UIApplication sharedApplication] openURL:url];
        }
    }
    
    return nil;
}

参考:http://stackoverflow.com/questions/30603671/open-a-wkwebview-target-blank-link-in-safa

你可能感兴趣的:(iOS WkWebview不支持 window.open的解决方法)