WKWebView

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSString *url = navigationAction.request.URL.absoluteString;

    if(![url isEqualToString:[NSString stringWithFormat:@"https://www.miospace.com/api/interface.php?method=h5.news_detail&id=%@",self.newsID]]) {

        NSArray *array = [url componentsSeparatedByString:@"id="];
        NewsDetailVC *vc = [[NewsDetailVC alloc]init];
        vc.newsID = array[1];
        [self.navigationController pushViewController:vc animated:YES];
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSString *url = navigationAction.request.URL.absoluteString;
    //如果是跳转一个新页面
    if (navigationAction.targetFrame == nil) {
        NSArray *array = [url componentsSeparatedByString:@"id="];
        
        NewsDetailVC *vc = [[NewsDetailVC alloc]init];
        vc.newsID = array[1];
        NSLog(@"398---%@",vc.newsID);
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
    decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSString *url = navigationAction.request.URL.absoluteString;
    //如果是跳转一个新页面
    NSString *subStr = @"id=";
    if ([url rangeOfString:subStr].location != NSNotFound) {
        NSArray *array = [url componentsSeparatedByString:@"id="];
        self.newsID = array[1];
        [self setData];
        [self updateNew];
        UIScrollView* scrollView = [[self.webView subviews] objectAtIndex:0];
        //CGPointMake(0, 0)回到顶部
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

你可能感兴趣的:(WKWebView)