本文介紹使用WKWebView攔截url進(jìn)行原生界面跳轉(zhuǎn)
- 使用代理方法decidePolicyForNavigationAction
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
// 獲取完整url并進(jìn)行UTF-8轉(zhuǎn)碼
NSString *strRequest = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
if ([strRequest hasPrefix:@"app://"]) {
// 攔截點(diǎn)擊鏈接
[self handleCustomAction:strRequest];
// 不允許跳轉(zhuǎn)
decisionHandler(WKNavigationActionPolicyCancel);
}else {
// 允許跳轉(zhuǎn)
decisionHandler(WKNavigationActionPolicyAllow);
}
}
- 自定義方法傳過來url進(jìn)行判斷二庵,需要html元素本身就有跳轉(zhuǎn)鏈接贪染,才可以攔截,如沒有催享,攔截不到杭隙。下文app://xxx鏈接為自定義鏈接
- (void)handleCustomAction:(NSString *)URL
{
// 判斷跳轉(zhuǎn)
NSString *link_id = @"";
if ([URL hasPrefix:@"app://video"]) {
// 視頻
MMLog(@"點(diǎn)擊了視頻%@",link_id);
}else if ([URL hasPrefix:@"app://item"]) {
// 單品
MMLog(@"點(diǎn)擊了單品%@",link_id);
}else if ([URL hasPrefix:@"app://brand"]) {
// 品牌
link_id = [URL substringFromIndex:[NSString stringWithFormat:@"app://brand"].length];
MMLog(@"點(diǎn)擊了品牌%@",link_id);
}
}
附上我的博客鏈接:oragekk'Blog 歡迎留言-不過評(píng)論系統(tǒng)換成了disqus需要搭梯子哦