作者:QuinceyYang (YangQing)
一、解决UIWebView页面不能调起微信支付
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx10103020268128bfe4d291131154467378&package=2397190279&redirect_url=http://www.soargift.com/parkApp/HuarunApp/orderDetail.html?parm=order_cwyd20180911001_a22
NSString *markString = request.URL.absoluteString;
NSLog(@"markString = %@",markString);
NSDictionary *headers = [request allHTTPHeaderFields];
BOOL hasReferer = [headers objectForKey:@"Referer"]!=nil;
if (hasReferer) {
// .. is this my referer?
if ([markString containsString:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"] && [markString containsString:@"http://www.soargift.com/parkApp/HuarunApp/orderDetail.html"]) {
NSRange tmpRange = [markString rangeOfString:@"&redirect_url=http://www.soargift.com/parkApp/HuarunApp/orderDetail.html"];
NSString *subStr1 = [markString substringToIndex:tmpRange.location];
NSString *subStr2 = [markString substringFromIndex:tmpRange.location+14];
[[NSUserDefaults standardUserDefaults] setObject:subStr2 forKey:@"H5PayFinishedRedirectUrl"];
[[NSUserDefaults standardUserDefaults] synchronize];
__weak __typeof(self)weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [NSURL URLWithString:subStr1];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
[request setValue:@"www.soargift.com://" forHTTPHeaderField:@"Referer"];
[weakSelf.webView loadRequest:request];
});
});
return NO;
}
return YES;
} else {
// relaunch with a modified request
__weak __typeof(self)weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [request URL];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
[request setValue:@"www.soargift.com://" forHTTPHeaderField:@"Referer"];
[weakSelf.webView loadRequest:request];
});
});
return NO;
}
return YES;
}
二、解决微信支付后跳回Safari问题
首先你在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“LSApplicationQueriesSchemes“添加weixin
再在URL Types中添加
三、解决跳回APP白屏问题,并显示支付结果
在对应的视图控制器中添加
//使用通知中心监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(h5PayFinishedGoback:) name:@"H5PayFinishedGoback" object:nil];
- (void)h5PayFinishedGoback:(NSNotification *)noti {
NSString *redirectUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"H5PayFinishedRedirectUrl"];
__weak __typeof(self)weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [NSURL URLWithString:redirectUrl];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
[request setValue:@"www.soargift.com://" forHTTPHeaderField:@"Referer"];
[weakSelf.webView loadRequest:request];
});
});
}
在APP跳转回调接口中添加
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
if ([url.scheme isEqualToString:@"www.soargift.com"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"H5PayFinishedGoback" object:nil];
return YES;
}
}