bugsWebview

WKWebView中a标签设置target问题

2019-12-07  本文已影响0人  Jack_King_
问题描述:

HTML代码中 a 标签设置 target 属性为 _blank,因为 _blank 属性在浏览器中代表打开一个新窗口,但在 WKWebView 中会导致 WKNavigationDelegate 导航方法被调用两次

<a style="color:#2c65bb; font-size:56px" href="demo://app/customer/home" target="_blank">打开链接</a>
navigationAction 分析:

1、首次打开h5页面,targetFrame request 为 null

<WKNavigationAction: 0x11de616f0; navigationType = -1; 
syntheticClickType = 0; position x = 0.00 y = 0.00 request = <NSMutableURLRequest: 0x280370b70> { URL: file:///private/var/containers/Bundle/Application/74B78B54-3ECE-422A-8A22-101AC2C6B3D2/Gundam.app/test.html }; 
sourceFrame = (null); 
targetFrame = <WKFrameInfo: 0x10a24ba00; webView = 0x10e00e200; isMainFrame = YES; request = (null)>>

2、点击 <a target="_blank" href= ".... > 标签

<WKNavigationAction: 0x10a50d290; navigationType = 0; syntheticClickType = 1; position x = 35.00 y = 1028.00 request = <NSMutableURLRequest: 0x28036d080> { URL: demo://app/customer/home };
sourceFrame = <WKFrameInfo: 0x10a50af70; webView = 0x10e00e200; isMainFrame = YES; request = <NSMutableURLRequest: 0x28036d0e0> { URL: file:///private/var/containers/Bundle/Application/74B78B54-3ECE-422A-8A22-101AC2C6B3D2/Gundam.app/test.html }>; 
targetFrame = (null)>
<WKNavigationAction: 0x10d90d730; navigationType = -1; syntheticClickType = 0; position x = 0.00 y = 0.00 request = <NSMutableURLRequest: 0x280374a30> { URL: demo://app/customer/home }; 
sourceFrame = (null); 
targetFrame = <WKFrameInfo: 0x10d903ae0; webView = 0x10e00e200; isMainFrame = YES; request = <NSMutableURLRequest: 0x2803749d0> { URL: file:///private/var/containers/Bundle/Application/74B78B54-3ECE-422A-8A22-101AC2C6B3D2/Gundam.app/test.html }>>
解决方案一

页面加载完成之后手动执行JS方法修改target属性为_self (如果页面有JS渲染需要延迟执行 target 属性修改)

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [webView evaluateJavaScript:@"var a = document.getElementsByTagName('a');for(var i=0;i<a.length;i++){a[i].setAttribute('target','_self');}" completionHandler:nil];
}
解决方案二

WKNavigationDelegate 导航方法中判断navigationAction.targetFrame属性

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    WKFrameInfo *frameInfo = navigationAction.targetFrame;
    // 判断frameInfo,处理业务逻辑代码
    decisionHandler(WKNavigationActionPolicyAllow);
}

上一篇下一篇

猜你喜欢

热点阅读