1、实现源码
// 处理拨打电话以及Url跳转等等
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL *URL = navigationAction.request.URL;
NSString *urlstr= [URL absoluteString];
NSString *scheme = [URL scheme];
if ([scheme isEqualToString:@"http"]||[scheme isEqualToString:@"https"]) {
// 拦截WKWebView加载的微信支付统一下单链接, 将redirect_url参数修改为唤起自己App的URLScheme
if ([urlstr containsString:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"]) {
if (!_payWx) {
_payWx = true;
decisionHandler(WKNavigationActionPolicyCancel);
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlstr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
newRequest.allHTTPHeaderFields = navigationAction.request.allHTTPHeaderFields;
newRequest.URL = [NSURL URLWithString:urlstr];
[newRequest setValue:@"www.shinetek.net.cn://" forHTTPHeaderField: @"Referer"];
[webView loadRequest:newRequest];
return;
} else {
_payWx = false;
}
}
// // 处理微信h5支付
// if ([urlstr containsString:@"tenpay"]) {
// _payString = urlstr;
// decisionHandler(WKNavigationActionPolicyCancel);
// return;
// }
}
//拦截重定向的跳转微信的 URL Scheme, 打开微信
if ([urlstr containsString:@"weixin://"]) {
decisionHandler(WKNavigationActionPolicyAllow);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
} else {
//未安装微信, 自行处理
}
});
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
设置Referer后微信支付结束可以跳转回app