美文网首页
App 内安装其他应用(WKWebView)

App 内安装其他应用(WKWebView)

作者: 小胖纸啦 | 来源:发表于2020-04-09 13:31 被阅读0次

    实现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);
    }
    

    相关文章

      网友评论

          本文标题:App 内安装其他应用(WKWebView)

          本文链接:https://www.haomeiwen.com/subject/uqqcmhtx.html