利用WKWebView加载网页,在app中处理H5页面调用微信支付 ------ 实现WKNavigationDelegate协议
1、由H5页跳转微信客户端--WKNavigationDelegate方法
该方法是决定H5页面是否允许跳转的
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler{
WKNavigationActionPolicy actionPolicy =WKNavigationActionPolicyAllow;//允许
NSString*urlScheme = navigationAction.request.URL.scheme;
NSString*urlString = navigationAction.request.URL.absoluteString;
NSString *wxPre = @"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb";
NSURLRequest*request = navigationAction.request;
NSLog(@"urlScheme:%@",urlScheme);
NSLog(@"urlStr:%@",urlString);
if([urlString containsString:@"weixin://wap/pay?"]) {
actionPolicy =WKNavigationActionPolicyCancel;//不允许跳转
NSURL*url = [NSURLURLWithString:urlString];
if([[UIApplicationsharedApplication]respondsToSelector:@selector(openURL:options:completionHandler:)]){
if(@available(iOS10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
}];
}else{
// Fallback on earlier versions
[[UIApplicationsharedApplication]openURL:url];
}
}else{
[[UIApplication sharedApplication]openURL:url];
}
}
decisionHandler(actionPolicy);//这句是必须加上的,不然会异常
}
2、如何从微信客户端跳转至app
很多小伙伴儿都遇到回跳的问题,点击“完成”或“取消”跳到了Safari却跳不回APP。
2.1、配置URL Types 添加schemes:www.xxxx.com
2.2、配置 Referer
[requestsetValue:@"www.suxiangshidai.com://" forHTTPHeaderField:@"Referer"];
2.3、服务器回调的URL中一定要写成 www.suxiangshidai.com:// , 如果一定要拼接,可以让后台区分一下,如果是iOS 前面一定要写成 www.suxiangshidai.com:// ,不然跳不回APP。
注意:www.xxxx.com 此域名一定是H5授权的域名,如果是二级域名,可以写成:aaa.xxxx.com
3、支付回来页面不刷新或产生空白页
一般支付完 不管完成与否都要调一下回调地址来刷新支付页面的,但问题是iOS的回调地址中一定要写成 www.xxxx.com:// 这样的格式才能跳回来,但是调回来后没有执行回调地址,页面又刷新不了,或者跳回来后是个空白页,这又产生了一个新的问题。当点击微信支付页上的“完成”或“取消”返回APP时,会调用 AppDelegate 中的 openURL: 方法
-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary *)options{
// url.scheme 就是 www.suxiangshidai.com,你只需要在这做一个判断,如果是 www.suxiangshidai.com 你就给外部发一个通知,那外部接收到这个通知,刷新界面就行了。
//刷新地址也就是支付完的回调地址,后台可以把回调地址拼接在 www.xxxx.com:// 后面,例如:www.xxxx.com://http://www.baidu.com 。 那么 url 就是 www.xxxx.com:// http://www.baidu.com ,你向外部发通知时把 url 作为参数传出去即可
if ([url.scheme containsString:@"www.suxiangshidai.com"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"weChatPayHandle" object:nil userInfo:@{@"url":url}];
}
return YES;
}
//微信支付后刷新主界面 (处理通知的方法 )
-(void)refreshOrderData:(NSNotification*)notifi{
NSDictionary*dic = notifi.userInfo;
NSString*url = [NSStringstringWithFormat:@"%@",dic[@"url"]];
NSRange range = [url rangeOfString:@"www.suxiangshidai.com://"];
NSString*lastUrl = [urlsubstringFromIndex:range.length];
NSLog(@"%@",lastUrl);
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:lastUrl]]];
}
利用WKWebView加载网页在app中处理H5页面调用支付宝支付
解决h5的支付宝支付问题 可以提供两种解决办法
第一种 调用支付宝网页支付
(问题 支付成功后回调到了游览器,无法返回APP,解决如下)
//以WKWebView为例
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
WKNavigationActionPolicy actionPolicy =WKNavigationActionPolicyAllow;//允许
NSString*urlScheme = navigationAction.request.URL.scheme;
NSString*urlString = navigationAction.request.URL.absoluteString;
NSString *wxPre = @"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb";
NSURLRequest*request = navigationAction.request;
NSLog(@"urlScheme:%@",urlScheme);
NSLog(@"urlStr:%@",urlString);
// 先判断一下,找到需要跳转的再做处理
if([urlSchemeisEqualToString:@"alipay"]) {
// 1.以?号来切割字符串
NSArray* urlBaseArr = [urlStringcomponentsSeparatedByString:@"?"];
NSString* urlBaseStr = urlBaseArr.firstObject;
NSString* urlNeedDecode = urlBaseArr.lastObject;
// 2.将截取以后的Str,做一下URLDecode,方便我们处理数据
// NSMutableString * afterDecodeStr = [NSMutableString stringWithString:[self URLDecodedString:urlNeedDecode]];
NSString*afterDecodeStr =WebURLDecodedString(urlNeedDecode);
// 3.替换里面的默认Scheme为自己的Scheme
NSString* afterHandleStr = [afterDecodeStrstringByReplacingOccurrencesOfString:@"alipays"withString:@"QEQuickEnjoyment"];
// 4.然后把处理后的,和最开始切割的做下拼接,就得到了最终的字符串
NSString* finalStr = [NSStringstringWithFormat:@"%@?%@",urlBaseStr,WebURLEncode(afterHandleStr)];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 判断一下,是否安装了支付宝APP(也就是看看能不能打开这个URL)
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:finalStr]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:finalStr]];
}
});
// 2.这里告诉页面不走了 -_-
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(actionPolicy);//这句是必须加上的,不然会异常
}
// OC 做URLEncode的方法
staticinlineNSString* WebURLEncode(NSString* str){
NSString*charactersToEscape =@"#[]@!$'()*+,;\"<>%{}|^~`";
NSCharacterSet*allowedCharacters = [[NSCharacterSetcharacterSetWithCharactersInString:charactersToEscape]invertedSet];
NSString *encodedUrl = [[str description] stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
return encodedUrl; }
staticinlineNSString*WebURLDecodedString(NSString* str)
{ return [str stringByRemovingPercentEncoding]; }
这样h5处理支付宝就可以了
但是如果想在支付宝成功后app处理事件 处理如下:
回到Appdelegate的代理方法
-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary *)options{// 判断一下这个host,safepay就是支付宝的if([url.host isEqualToString:@"safepay"]) {// 拿到这个链接的数据,数据大概是这样的/*
mushRoomBox://safepay/?%7B%22memo%22:%7B%22result%22:%22%7B%5C%22alipay_trade_wap_pay_response%5C%22:%7B%5C%22code%5C%22:%5C%2210000%5C%22,%5C%22msg%5C%22:%5C%22Success%5C%22,%5C%22app_id%5C%22:%5C%222017020505522491%5C%22,%5C%22auth_app_id%5C%22:%5C%222017020505522491%5C%22,%5C%22charset%5C%22:%5C%22GBK%5C%22,%5C%22timestamp%5C%22:%5C%222018-11-09%2011:43:06%5C%22,%5C%22out_trade_no%5C%22:%5C%222018100001_simple_48f033a4a00ba19a0526ddbd4f96b036%5C%22,%5C%22total_amount%5C%22:%5C%220.01%5C%22,%5C%22trade_no%5C%22:%5C%222018110922001438371006963978%5C%22,%5C%22seller_id%5C%22:%5C%222088221175674834%5C%22%7D,%5C%22sign%5C%22:%5C%22T6jn7QSsvjjG%5C/HxknSl2lOdHj6zf7jebWEKkeGhEJaXCYFbRHMTsml7TNRhRACupmwDYUADQOynKNR%5C/QBXHgBxJRhYEE9SXVz4mp7sqSGPItzV4bVmZw5AG5UJzsuIh9O6QC%5C/QHc+FcA2i1zw46VDMUnbCCKat5q4ME9d3AUSMhYg31xO+1TZnraDfm0I%5C/OwAnA5iCTOhq%5C/fkm%5C/r4hsujdv9fTvmszgIJLMhN7yQhoGaOWs0vIQ6hoJoXmCXVapfY7ANsoKaR6hGJogH4GKGQi0vLk17W8s+LjcOUT%5C/zB6K6gN+fyCefm8FOJ+8g4GUAreuZDcW0MuGE1OOmaxDrXg==%5C%22,%5C%22sign_type%5C%22:%5C%22RSA2%5C%22%7D%22,%22ResultStatus%22:%229000%22,%22memo%22:%22%22%7D,%22requestType%22:%22safepay%22%7D
*/// 然后,一样的套路方法
NSString* urlNeedJsonStr = url.absoluteString;
NSArray* afterComStr = [urlNeedJsonStr componentsSeparatedByString:@"?"];/
/ 这个decode方法,在上面找哈NSString* lastStr = [self WebURLDecodedString:afterComStr.lastObject];// 这个lastStr,其实是一个jsonStr,转一下,就看到了数据
NSDictionary* dict = [self dictionaryWithJsonString:lastStr];// dict的结构差不多是这样/*
"memo": {
"result":"订单相关信息,如订单号,支付金额等等";
"ResultStatus":"9000";
},
******
*/// 和支付宝SDK的返回结果一次,这个ResultStatus,就是我们要的数据// 9000 :支付成功// 8000 :订单处理中// 4000 :订单支付失败// 6001 :用户中途取消// 6002 :网络连接出错// 这里的话,就可以根据状态,去处理自己的业务了}returnYES;}
- (NSDictionary*)dictionaryWithJsonString:(NSString*)jsonString{
if(jsonString ==nil) {
return nil;
}
NSData*jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError*err;NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:jsonData options:NSJSONReadingMutableContainerserror:&err];
if(err)
{NSLog(@"json解析失败:%@",err);
return nil;
}return dic;
}
第二种解决办法
接支付宝提供的SDK,schemeStr参数传入自己APP的URLScheme即可。 具体的 手机网站支付转Native支付
1.1、掉起支付宝APP
1.2、支付完成后,支付宝APP要返回到我们的APP里面来
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler{
BOOL isIntercepted = [[AlipaySDK defaultService] payInterceptorWithUrl:[navigationAction.request.URL absoluteString] fromScheme:@"WRWarehouseHasRice" callback:^(NSDictionary *result) {
// 处理支付结果
NSLog(@"%@", result);
// isProcessUrlPay 代表 支付宝已经处理该URL
if([result[@"isProcessUrlPay"]boolValue]) {
// returnUrl 代表 第三方App需要跳转的成功页URL
NSString*urlStr = result[@"returnUrl"];
}
}];
if(isIntercepted) {
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
网友评论