解决H5微信支付返回跳转问题
注册域名坑:请使用www.xxx.com(别使用abc.m.en.com)这种形式向微信注册,否则无法正常回调处理
使用iOS打开微信支付页面需要设置webview的Referer
Referer一般为http://www.xxx.com或者直接www.xxx.com
在Info中URL Type添加该Referer为打开自己app的scheme
嗯,就这样结束!!!
现的是通过拦截web请求,判断是否为https/http请求,不是则通过scheme方式打开外部页面:标准写法
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL *url = navigationAction.request.URL;
NSString *scheme = [url scheme];
if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
[[UIApplication sharedApplication] openURL:url];
}
decisionHandler(WKNavigationActionPolicyAllow);
}
打开微信可能遇到的坑下方代码不但实现页面跳转,还处理了web url不是微信注册的refer问题
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {NSURLRequest *request = navigationAction.request;
NSString *scheme = [request.URL scheme];
//微信支付
if ([scheme isEqualToString:@"weixin"]) {
decisionHandler(WKNavigationActionPolicyCancel);
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:request.URL];
if (canOpen) {
[[UIApplication sharedApplication] openURL:request.URL];
}
return;
}NSDictionary *referDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"loadUrlWithRefer"];
if (referDict != nil && [referDict isKindOfClass:[NSDictionary class]]) {
NSString *url = referDict[@"url"];
NSString *refer = referDict[@"refer"];
NSDictionary *headers = [navigationAction.request allHTTPHeaderFields];
NSString * referer = [headers objectForKey:@"Referer"];
BOOL isRightReferer = [referer isEqualToString:refer];
if (isRightReferer) {
decisionHandler(WKNavigationActionPolicyAllow);
} else {
decisionHandler(WKNavigationActionPolicyCancel);
if ([url isEqualToString:[navigationAction.request.URL absoluteString]]) {
NSMutableDictionary * muDict = [[NSMutableDictionary alloc] initWithDictionary:headers];
[muDict setObject:refer forKey:@"Referer"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [navigationAction.request URL];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
request.allHTTPHeaderFields = muDict;
[webView loadRequest:request];
});
});
}
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loadUrlWithRefer"];
return;
}
}
decisionHandler(WKNavigationActionPolicyAllow);
}
loadUrlWithRefer存储的内容为web页面请求的订单信息通过js传递到原生并存储,包括注册微信的url和refer
【解决H5微信支付返回跳转问题】//微信支付
https://wxpay.wxutil.com/mch/pay/h5.v2.php
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- 基于微信小程序带后端ssm接口小区物业管理平台设计
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 员工的微信朋友圈是公司的宣传阵地吗()
- FBI怎么和恐怖分子谈判
- Spark|Spark 数据倾斜及其解决方案
- 解决SyntaxError:|解决SyntaxError: invalid syntax