攻城狮@IT·互联网程序员

WKWebView拦截URL

2017-05-31  本文已影响653人  上冬十二

本文介绍使用WKWebView拦截url进行原生界面跳转

3.gif
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    // 获取完整url并进行UTF-8转码
    NSString *strRequest = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
    if ([strRequest hasPrefix:@"app://"]) {
        // 拦截点击链接
        [self handleCustomAction:strRequest];
        // 不允许跳转
        decisionHandler(WKNavigationActionPolicyCancel);
    }else {
        // 允许跳转
        decisionHandler(WKNavigationActionPolicyAllow);
        
    }
}
- (void)handleCustomAction:(NSString *)URL
{
    // 判断跳转
    NSString *link_id = @"";
    if ([URL hasPrefix:@"app://video"]) {
        // 视频
        MMLog(@"点击了视频%@",link_id);
    }else if ([URL hasPrefix:@"app://item"]) {
        // 单品
        MMLog(@"点击了单品%@",link_id);
    }else if ([URL hasPrefix:@"app://brand"]) {
        // 品牌
        link_id = [URL substringFromIndex:[NSString stringWithFormat:@"app://brand"].length];
        MMLog(@"点击了品牌%@",link_id);
    }
}

附上我的博客链接:oragekk'Blog 欢迎留言-不过评论系统换成了disqus需要搭梯子哦

上一篇下一篇

猜你喜欢

热点阅读