037-iOS解决微信h5支付无法直接返回APP的问题

一、配置微信白名单

首先你在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“LSApplicationQueriesSchemes“添加weixin

image.png

二、配置APP的URL Scheme

配置APP的URL Scheme

特别注意,能成功返回的关键在于URL Scheme的填写内容!

假设你想要App标识为abc,公司域名为baidu.com。那么URL Scheme就为:abc.baidu.com

其中,公司域名应与在微信商户平台填写的一致才能正常支付。如需添加或修改授权域名申请H5支付时提交的授权域名,请登陆商户号对应的商户平台--"产品中心"--"开发配置"自行配置!

image.png

三、APP端代码

APP端代码

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *str = request.URL.absoluteString;
    if ([str hasPrefix:@"https://wx.tenpay.com"]) {
        NSDictionary *headers = [request allHTTPHeaderFields];
        NSString *string = [NSString stringWithFormat:@"%@",[headers objectForKey:@"Referer"]];
        if ([string hasPrefix:@"http"] || [string containsString:@"null"] || string.length == 0) {
            NSURL *url = [request URL];
            NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
            NSArray *tmpArray = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleURLTypes"];
            for (NSDictionary *tmpDic in tmpArray) {
                NSString *CFBundleURLName = [NSString stringWithFormat:@"%@",tmpDic[@"CFBundleURLName"]];
                if ([CFBundleURLName isEqualToString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]]) {
                    NSArray *CFBundleURLSchemesArray = [NSArray arrayWithArray:tmpDic[@"CFBundleURLSchemes"]];
                    if (CFBundleURLSchemesArray.count > 0) {
                        NSString *needString = [NSString stringWithFormat:@"%@",CFBundleURLSchemesArray[0]];
                        [request setValue:[NSString stringWithFormat:@"%@://",needString] forHTTPHeaderField:@"Referer"];
                        [self.webView loadRequest:request];
                    }
                    break;
                }
            }
            
        }
    }
    return YES;
}

这样,不仅能够正常支付,在微信支付界面也能直接跳转回APP了。

你可能感兴趣的:(037-iOS解决微信h5支付无法直接返回APP的问题)