3.gif本文介绍使用WKWebView拦截url进行原生界面跳转
- 使用代理方法decidePolicyForNavigationAction
- (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);
}
}
- 自定义方法传过来url进行判断,需要html元素本身就有跳转链接,才可以拦截,如没有,拦截不到。下文app://xxx链接为自定义链接
- (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需要搭梯子哦
网友评论