IOS 关于web支付无法返回APP解决方案

使用WKWebview支付,使用的支付宝和微信支付,由服务器下单,app端拿到服务端下单数据加载,微信支付下单成功返回的是一个链接,支付宝支付下单成功返回的是一个html样式,加载方式也是不同的

//拿到服务端下单数据之后
 dispatch_async(dispatch_get_main_queue(), ^{
        if (weakSelf.paytype == WeChatPay) {
        //微信支付
            NSString*dataStr = [self.dataDic objectForKey:@"web_url"];
            if(dataStr.length==0){
                return ;
            }
            [self loadRequestWith:dataStr];
        }else if(weakSelf.paytype == AliPay){
        //支付宝支付
             NSString*dataStr = [self.dataDic objectForKey:@"AlipayTradeWapPayRequest"];
            if (dataStr.length==0) {
                return ;
            }
             [weakSelf.webView loadHTMLString:dataStr baseURL:nil];
        }

    });

微信支付请求处理

-(void)loadRequestWith:(NSString*)urlStr{
    NSLog(@"%@",urlStr);

    // urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    urlStr = [self URLEncodedStringWith:urlStr];
    self.requestUrlStrs = urlStr;//@"https://www.baidu.com";//

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.requestUrlStrs]];
    request.HTTPMethod = @"GET";
    //设置Preferer 微信支付能跳转回来
   /*schemUrlStr Info里面注册的Scheme 
   *company.com是公司注册支付时候的域名,A.company.com
   *A 可以任意非特殊字符的字符串*/
    NSString*schemUrlStr = @"A.company.com://";
     [request setValue:schemUrlStr forHTTPHeaderField:@"Referer"];
    dispatch_async(dispatch_get_main_queue(), ^{
    [self.webView loadRequest:request];
    });

}

设置SchemUrl
IOS 关于web支付无法返回APP解决方案_第1张图片

获取跳转的Url 实现跳转到微信或者支付宝

-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    NSString *url = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
    NSString* reUrl=[[webView URL] absoluteString];
    reUrl = url;
//    NSURLRequest *request = navigationAction.request;
//    NSString *wxPre = @"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb";
    if ([url containsString:@"alipay://"]) {
        //支付宝支付
        if ([url containsString:@"\"fromAppUrlScheme\":\"alipays\""]) {
        //抓取fromAppUrlScheme 将alipays 替换成自己设置的
             NSString*schemUrlStr = @"A.company.com://";
            NSString*replaceStr = [NSString stringWithFormat:@"\"fromAppUrlScheme\":\"%@\"",schemUrlStr];
             url = [url stringByReplacingOccurrencesOfString:@"\"fromAppUrlScheme\":\"alipays\"" withString:replaceStr];
        }
        NSInteger subIndex = 23;
        NSString* dataStr=[url substringFromIndex:subIndex];
        //编码
        NSString *encodeString = [self URLEncodedStringWith:dataStr];
        NSMutableString* mString=[[NSMutableString alloc] init];
        [mString appendString:[url substringToIndex:subIndex]];
        [mString appendString:encodeString];

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mString]];

    }else if([url  hasPrefix:@"weixin://"]){

        if ([[UIApplication sharedApplication]canOpenURL:navigationAction.request.URL]) {
            //微信支付
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication]openURL:navigationAction.request.URL options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) {
                }];
            } else {
                // Fallback on earlier versions
            }
        }else{
            [[UIApplication sharedApplication]openURL:navigationAction.request.URL];

        }
    }
     decisionHandler(WKNavigationActionPolicyAllow);
    return;
}
//Url 处理
- (NSString *)URLEncodedStringWith:(NSString*)urlStr
{
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)urlStr,
                                                              (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
                                                              NULL,
                                                              kCFStringEncodingUTF8));
    return encodedString;
}

微信和支付宝都可以跳转了

iOS支付宝H5支付无法返回APP解决方案

iOS微信H5支付无法返回APP解决方案

你可能感兴趣的:(IOS,IOS开发)