解决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

    推荐阅读