iOS CodingiOS 技术精选集

iOS开发-微信支付宝H5支付跳转原生APP的问题

2018-07-13  本文已影响0人  看我的大白眼

参考

上面的3个博客可以完美解决问题,我只是稍作了修改

支付宝处理

webView拦截 alipay://alipayclient 请求,追加或修改参数 fromAppUrlScheme为你自己的URLScheme值,生成新的NSURL,然后用 [[UIApplication sharedApplication] openURL:] 打开即可。
处。


if ([[URL absoluteString] hasPrefix:@"alipays://"] || [[URL absoluteString] hasPrefix:@"alipay://"]) {
        
        NSString *urlStr = [NSString stringWithFormat:@"%@",URL];
        NSMutableString *newUrlStr = [[NSMutableString alloc]initWithString:urlStr];
        if ([urlStr containsString:@"fromAppUrlScheme"] && [urlStr containsString:@"alipays"]) {
            NSRange range = [newUrlStr rangeOfString:@"alipays"];
            [newUrlStr replaceCharactersInRange:range withString:@"你的URLScheme"];
        }

        NSURL *newUrl = [NSURL URLWithString:newUrlStr];
        
        // NOTE: 跳转支付宝App
        BOOL bSucc = [[UIApplication sharedApplication]openURL:newUrl];
        
        // NOTE: 如果跳转失败,则跳转itune下载支付宝App
        if (!bSucc) {

        }
        decisionHandler(WKNavigationActionPolicyCancel);
        return ;
    }

微信支付处理

主要参考https://www.jianshu.com/p/90db7dfb075c 部分修改了

这里没有让后台处理回调url,iOS自己截取字符串,回调url拼接到了授权域名后面

if ([URL.absoluteString containsString:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?"]) {


        if ([[UIApplication sharedApplication] canOpenURL:URL]) {

            #warning 微信支付链接不要拼接redirect_url,如果拼接了还是会返回到浏览器的
            NSArray *array = [URL.absoluteString componentsSeparatedByString:@"&redirect_url="];
            //传入的是微信支付链接:https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx201801291021026cb304f9050743178155&package=3456576571
            //这里把webView设置成一个像素点,主要是不影响操作和界面,主要的作用是设置referer和调起微信
            WebChatPayH5VIew *h5View = [[WebChatPayH5VIew alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
            //url是没有拼接redirect_url微信h5支付链接
            [h5View loadingURL:array.firstObject withIsWebChatURL:NO host:array.lastObject];
            [self.view addSubview:h5View];
            decisionHandler(WKNavigationActionPolicyCancel);
        }else{

        }
        return;
    }

这样在微信跳转回来的时候AppDelegate中就可以获取到之前的回调地址

 // relaunch with a modified request
        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 setValue:[NSString stringWithFormat:@"xxx.com://%@",self.host] forHTTPHeaderField: @"Referer"];
                [self.myWebView loadRequest:request];
            });
        });

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    NSString *urlStr = [NSString stringWithFormat:@"%@",url];
    if ([urlStr rangeOfString:@"xxx.com"].location != NSNotFound) {
        NSString *newUrlStr = url.host;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"WXPayNotification" object:nil userInfo:@{@"url":newUrlStr}];
    }
    return  YES;
    
}

这样H5控制器接到通知之后,重新加载URL就可以了

上一篇下一篇

猜你喜欢

热点阅读