- (void)webView:(WKWebView *)theWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
NSString *urlStr = navigationAction.request.URL.absoluteString;
//微信支付需要用的的Scheme 需要配置在Info.plist里
NSString *appScheme = @"微信后台注册";
NSString *CompanyFirstDomainByWeChatRegister = @"商户APP设置自己的schema";
//支付宝跳转回来用的Scheme 需要配置在Info.plist里
NSString *exeScheme = [ThirdKeyHelper exePayKey];
static NSString *endPayRedirectURL = nil;
NSURLRequest *request = navigationAction.request;
#pragma mark 支付宝支付
if ([urlStr hasPrefix:@"alipays://"] || [urlStr hasPrefix:@"alipay://"]) {
NSURL *openedURL = navigationAction.request.URL;
NSString *prefixString = @"alipay://alipayclient/?";
NSString *urlString = [[self xh_URLDecodedString:urlStr] stringByReplacingOccurrencesOfString:@"alipays" withString:exeScheme];
;
if ([urlString hasPrefix:prefixString]) {
NSRange rang = [urlString rangeOfString:prefixString];
NSString *subString = [urlString substringFromIndex:rang.length];
NSString *encodedString = [prefixString stringByAppendingString:[self xh_URLEncodedString:subString]];
openedURL = [NSURL URLWithString:encodedString];
}
BOOL isSucc = [[UIApplication sharedApplication] openURL:openedURL];
if (isSucc) {
NSLog(@"未安装某宝客户端");
}
decisionHandler(WKNavigationActionPolicyCancel);
return;
#pragma mark 微信支付
} else if ([absoluteString hasPrefix:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"] &&
![absoluteString hasSuffix:[NSString stringWithFormat:@"redirect_url=%@://",CompanyFirstDomainByWeChatRegister]]
&& ![absoluteString hasSuffix:[NSString stringWithFormat:@"redirect_url=%@://",appScheme]]) {
decisionHandler(WKNavigationActionPolicyCancel);
NSString *redirectUrl = nil;
if ([absoluteString containsString:@"redirect_url="]) {
NSRange redirectRange = [absoluteString rangeOfString:@"redirect_url"];
endPayRedirectURL = [absoluteString substringFromIndex:redirectRange.location+redirectRange.length+1];
redirectUrl = [[absoluteString substringToIndex:redirectRange.location] stringByAppendingString:[NSString stringWithFormat:@"redirect_url=%@://",appScheme]];
}else {
redirectUrl = [absoluteString stringByAppendingString:[NSString stringWithFormat:@"&redirect_url=%@://",appScheme]];
}
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:redirectUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
newRequest.allHTTPHeaderFields = request.allHTTPHeaderFields;
newRequest.URL = [NSURL URLWithString:redirectUrl];
//appScheme和referer存在对应关系,且一级域名必须是相同的
// xxxxx.xxxx.com://
[newRequest addValue:@"支付请求头,后台提供" forHTTPHeaderField:@"Referer"];
[self.webView loadRequest:newRequest];
return;
}
// Judge is whether to jump to other app.
if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"] && [scheme isEqualToString:@"weixin"]) {
decisionHandler(WKNavigationActionPolicyCancel);
if ([scheme isEqualToString:@"weixin"]) {
if (endPayRedirectURL) {
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:endPayRedirectURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]];
} }
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:request.URL];
if (canOpen) {
[[UIApplication sharedApplication] openURL:request.URL];
[self close];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:CustomStr(@"未安装微信") preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:CustomStr(@"确定") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self close];
}];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
return;
}
}
可参考 https://blog.csdn.net/qq_34873211/article/details/80095573