1、首先用到webview的代理
//在发送请求之前,决定是否跳转
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSURLRequest *request = navigationAction.request;
NSString *scheme = [request.URL scheme];
NSString *absoluteString = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
NSLog(@"Current URL is %@",absoluteString);
static NSString *endPayRedirectURL = nil;
if ([absoluteString hasPrefix:@"alipays://"] || [absoluteString hasPrefix:@"alipay://"])
{
NSURL *openedURL = navigationAction.request.URL;
NSString *prefixString = @"alipay://alipayclient/?";
//替换里面的默认Scheme为自己的Scheme
NSString *urlString = [[self xh_URLDecodedString:absoluteString] stringByReplacingOccurrencesOfString:@"alipays" withString:[NSString stringWithFormat:@"%@",LHAPIUrl.wechatpayRegisterUrl]];
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;
}
//h5 vxxx 拦截
if ([absoluteString hasPrefix:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"]) {
//微信回调地址,微信支付完成后将通过此地址返回APP,并且需要和URL Scheme配置一样
NSString *wxCallbackUrlStr = LAPIUrl.wechatpayRegisterUrl;
//结尾不是... 并且包含...
if (![absoluteString hasSuffix:[NSString stringWithFormat:@"redirect_url=%@://",wxCallbackUrlStr]] &&
[absoluteString containsString:wxCallbackUrlStr]) {
decisionHandler(WKNavigationActionPolicyCancel);
NSString *redirectUrl = nil;
self.wichStyle = @"weixin";
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=%@://",wxCallbackUrlStr]];
}else {
redirectUrl = [absoluteString stringByAppendingString:[NSString stringWithFormat:@"&redirect_url=%@://",LHAPIUrl.wechatpayRegisterUrl]];
}
//赋值
self.endHtmlUl = endPayRedirectURL;
NSURL * url = [NSURL URLWithString:@"weixin://"];
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:url];
if (canOpen)
{
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:redirectUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
newRequest.allHTTPHeaderFields = request.allHTTPHeaderFields;
newRequest.URL = [NSURL URLWithString:redirectUrl];
[webView loadRequest:newRequest];
}else {
[LProgressHUD showWaitingForMessage:@"请检测是否安装微信"];
}
return;
}else {
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
}
///额外:-- 跳转并打开其他APP
if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
decisionHandler(WKNavigationActionPolicyCancel);
if ([scheme isEqualToString:@"weixin"]) {
self.wichStyle = @"weixin";
if (endPayRedirectURL) {//endPayRedirectURL
NSLog(@"取消1")
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.currentHtmlUl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20]];
}
}else if ([scheme isEqualToString:LHAPIUrl.wechatpayRegisterUrl]) {
self.wichStyle = @"weixin";
}else if ([scheme isEqualToString:@"imeituan"]) {
[[UIApplication sharedApplication] openURL:request.URL options:@{} completionHandler:nil];
return;
}
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:request.URL];
if (canOpen) {
[[UIApplication sharedApplication] openURL:request.URL options:@{} completionHandler:nil];
}
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
2、在URL Scheme设置,和上面的wxCallbackUrlStr保持一致,设置后可使用Safari测试,输入xxxxxxxxx://
image.png
3、跳转并打开其他APP需添加的白名单
<key>LSApplicationQueriesSchemes</key>
<array>
<string>pinduoduo</string>
<string>weixin</string>
<string>meituan</string>
<string>meituanwaimai</string>
<string>wechat</string>
<string>weixinULAPI</string>
<string>jdlogin</string>
<string>openapp.jdmobile</string>
<string>tbopen</string>
<string>tmall</string>
<string>alipay</string>
<string>iosamap</string>
<string>imeituan</string>
<string>imeituan://</string>
</array>
网友评论