实现WKNavigationDelegate代理方法
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
WKNavigationActionPolicy policy =WKNavigationActionPolicyAllow;
NSString *strRequest = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
strRequest = [strRequest stringByReplacingOccurrencesOfString:@"///" withString:@"//"];
// 在App Store中下载
if([[navigationAction.request.URL host] isEqualToString:@"itunes.apple.com"] && [[UIApplication sharedApplication] openURL:navigationAction.request.URL] ){
policy = WKNavigationActionPolicyCancel;
}
// 企业证书App通用前缀
NSString *hasPrefix = @"itms-services://?action=download-manifest&url=";
// 下载其他产品app
if ([strRequest containsString:hasPrefix]) {
/**
url 后面的链接地址https://download.fir.im/apps/xxx/install?download_token=xxxxxx,
需要 URLEncode 成 https%3A%2F%2Fdownload.fir.im%2Fapps%2F%3Aid%2Finstall%3Fdownload_token%3Dxxxxxx
*/
NSRange range = [strRequest rangeOfString:hasPrefix];
NSString *str = [strRequest substringFromIndex:range.length];
// 转码后的地址
[self openDownLoadUrl:[NSString stringWithFormat:@"%@%@", hasPrefix, [str encodeString]]];
}
// 若是安装描述文件证书,退出当前控制器
if ([strRequest hasSuffix:@".mobileprovision"]) {
[self.navigationController popViewControllerAnimated:YES];
}
decisionHandler(policy);
}
网友评论