iOS development --- The problem of unable to jump when opening internal links in WKWebView

Problem Description 

After WKWebView loads the link, clicking on the internal link cannot jump. This is because the target = "_black" in <a href = "xxx" target = "_black"> opens a new page, so it cannot be opened on the current page. It needs to be opened on the current page. Page reload url.

a hyperlink target:
  _blank -- open the link in a new window
  _parent -- open the link in the parent form
  _self -- open the link in the current form, this is the default value
  _top -- open the link in the current form, and Replace the entire current form (frame page)

Solution

Method 1 WkUIDelegate:

/**
<WKUIDelegate>
wkWebView.UIDelegate = self;
*/

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

Method 2 WKNavigationDelegate:

/**
<WKNavigationDelegate>
wkWebView.navigationDelegate = self;
*/
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    //如果是跳转一个新页面
    if (navigationAction.targetFrame == nil) {
        [webView loadRequest:navigationAction.request];
    }
 
    decisionHandler(WKNavigationActionPolicyAllow);
}

 

Guess you like

Origin blog.csdn.net/jiaxin_1105/article/details/107659309