美文网首页
iOS webView超链接拦截相关

iOS webView超链接拦截相关

作者: JohnayXiao | 来源:发表于2018-11-27 15:48 被阅读21次
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
        
        NSLog(@"开始请求: %@ \n\n", navigationAction.request);
        NSLog(@"navigationAction: %@ \n\n", navigationAction);
        
        [self showBigImage:navigationAction.request];
        
        NSString *urlString = [navigationAction.request.URL absoluteString];
        if ([urlString containsString:@"m-web/info/infodetail/"]) {
            
            HPNewsDetailNewVC *newsD = [[HPNewsDetailNewVC alloc] initWithNewsID:[urlString componentsSeparatedByString:@"/"].lastObject];
            [self.navigationController pushViewController:newsD animated:YES];
            
            // 不允许web内跳转
            decisionHandler(WKNavigationActionPolicyCancel);
            
        }else if ([urlString containsString:@"m-web/activity/detail/"]) {
            
            HPAcitivityNewDetailVC *nv = [[HPAcitivityNewDetailVC alloc] initWithID:[urlString componentsSeparatedByString:@"/"].lastObject];
            [self.navigationController pushViewController:nv animated:YES];
            
            // 不允许web内跳转
            decisionHandler(WKNavigationActionPolicyCancel);
            
        }else if ([urlString containsString:@"m-web/train/all/"]) {
            
            HPTrainDetailVC2 *vc = [[HPTrainDetailVC2 alloc] initWithCourseId:[urlString componentsSeparatedByString:@"/"].lastObject courseGuid:nil courseApplyId:nil];
            [self.navigationController pushViewController:vc animated:YES];
            
            // 不允许web内跳转
            decisionHandler(WKNavigationActionPolicyCancel);
            
        }else {
            
            decisionHandler (WKNavigationActionPolicyAllow);
        }
        
    //    if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {//跳转别的应用如系统浏览器
    //        // 对于跨域,需要手动跳转
    ////        [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
    //
    //        //    2.跳转别的应用如系统浏览器(跨域跳转)、应用的web内跳转(不跨域跳转)
    //        //    NSString *hostname = navigationAction.request.URL.host.lowercaseString;
    //
    //    } else {//应用的web内跳转
    //        decisionHandler (WKNavigationActionPolicyAllow);
    //    }
       
    }
    

    相关文章

      网友评论

          本文标题:iOS webView超链接拦截相关

          本文链接:https://www.haomeiwen.com/subject/rhngqqtx.html