监听网页中的返回

2018-11-13  本文已影响8人  边河

也就是html中的history.go(-1)

html代码

<button class="btn btn_primary" onclick="goBack()">返回</button>

function goBack()
{
   window.history.go(-1);
}

这时候网页监听便会走

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if (navigationAction.navigationType == WKNavigationTypeBackForward) {
       NSLog(@"goback");
   }
  decisionHandler(WKNavigationActionPolicyAllow);
}

打印出goback

网页的历史记录

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if (navigationAction.navigationType==WKNavigationTypeBackForward) {                 
        if (webView.backForwardList.backList.count>0) {                                  
            WKBackForwardListItem * item = webView.backForwardList.currentItem;         
            for (WKBackForwardListItem * backItem in webView.backForwardList.backList) { 
                [webView goToBackForwardListItem:[webView.backForwardList.backListfirstObject]];
            }
        }
    }
decisionHandler(WKNavigationActionPolicyAllow);
}

上一篇 下一篇

猜你喜欢

热点阅读