WKWebView企业包和AppStore包

H5代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>123</title>
</head>
<body>
<input type="button" value="iOS下载" onclick="download()">
</body>

<script>
function download() {
window.location.href="itms-services://?action=download-manifest&url=https://xx.com/xx.plist";
}
</script>

</html>

WKWebView代码

1、初始化

1
2
3
4
let frame = CGRect(x: 0, y: 0, width: UIScreen.w, height: UIScreen.h)
let webView = WKWebView(frame: frame)
webView.navigationDelegate = self
webView.uiDelegate = self

2、加载上述H5链接

1
2
3
4
guard let url = URL(string: urlString) else {return}
let request = URLRequest(url: url)
webView.load(request)
view.addSubview(webView)

3、使用代理拦截
1、首先遵守协议WKNavigationDelegate,WKUIDelegate
2、点击H5上面的iOS下载按钮时,会把按钮下载链接传到下面的代理方法,进行拦截处理就好了
swift代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url else {
decisionHandler(.allow)
return
}
guard let scheme = url.scheme else {
decisionHandler(.allow)
return
}
if scheme == "itms-services" { // 需要跳转到企业包
decisionHandler(.cancel)
UIApplication.shared.openURL(url)
return
}
guard let host = url.host else {
decisionHandler(.allow)
return
}
if host == "itunes.apple.com" { // 需要跳转到AppStore
decisionHandler(.cancel)

UIApplication.shared.openURL(url)
if webView.canGoBack {
webView.goBack()
} else {
_ = navigationController?.popViewController(animated: true)
}
} else {
decisionHandler(.allow)
}
}

oc代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSURL *url = navigationAction.request.URL;
if ([url.scheme isEqualToString:@"itms-services"] ) { // 需要跳转到企业包
decisionHandler(WKNavigationActionPolicyCancel);
[UIApplication.sharedApplication openURL:url];
return;
}
if ([url.host isEqualToString:@"itunes.apple.com"]) {
decisionHandler(WKNavigationActionPolicyCancel);
[UIApplication.sharedApplication openURL:url];
if (webView.canGoBack) {
[webView goBack];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
如果这篇文章对您有帮助,请支持我继续创作!