iOS 13通过设置Scheme来调起第三方App,在第三方App完成操作后,回到App内部,系统回调不同步的解决方案

最近项目使用Xcode11创建的,创建完成后系统会自带AppDelegate和Xcode 11中带有的支持多屏幕配置的SceneDelegate。目录结构如下:


595477FC-950F-4E63-8300-97A873DCCDB0.png

一、支付宝支付,按照我们之前的处理方式是在AppDelegate的下面方法中做回调处理:

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付,处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            NSString *payStatus = resultDic[@"resultStatus"];
            if ([payStatus isEqualToString:@"9000"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"success"];
                
            } else if ([payStatus isEqualToString:@"6001"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"cancel"];
                
            } else if ([payStatus isEqualToString:@"8000"] || [payStatus isEqualToString:@"6004"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"confirm"];
            } else {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"fail"];
            }
        }];
        
        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            // 解析 auth code
            NSString *result = resultDic[@"result"];
            NSString *payStatus = resultDic[@"resultStatus"];
            NSString *authCode = nil;
            if (result.length>0) {
                NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                for (NSString *subResult in resultArr) {
                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                        authCode = [subResult substringFromIndex:10];
                        break;
                    }
                }
            }
            NSLog(@"授权结果 authCode = %@", authCode?:@"");
            
            if ([payStatus isEqualToString:@"9000"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"success"];
                
            } else if ([payStatus isEqualToString:@"6001"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"cancel"];
                
            } else if ([payStatus isEqualToString:@"8000"] || [payStatus isEqualToString:@"6004"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"confirm"];
            } else {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"fail"];
            }
        }];
    }
    
    // 此处是微信支付
    if ([url.scheme isEqualToString:WXAppId]) {
        return  [WXApi handleOpenURL:url delegate:self];
    } else {
        // 银联支付回调
        [[UPPaymentControl defaultControl] handlePaymentResult:url completeBlock:^(NSString *code, NSDictionary *data) {
            if ([code isEqualToString:@"success"]) {
                // 结果code为成功时,去商户后台查询一下确保交易是成功的再展示成功
                NSNotification *notification = [NSNotification notificationWithName:@"ORDER_UNIONPAY_NOTIFICATION" object:@"success"];
                [[NSNotificationCenter defaultCenter] postNotification:notification];
                
            } else if ([code isEqualToString:@"fail"]) {
                // 交易失败
                NSNotification *notification = [NSNotification notificationWithName:@"ORDER_UNIONPAY_NOTIFICATION" object:@"fail"];
                [[NSNotificationCenter defaultCenter] postNotification:notification];
            } else if ([code isEqualToString:@"cancel"]) {
                // 交易取消
                NSNotification *notification = [NSNotification notificationWithName:@"ORDER_UNIONPAY_NOTIFICATION" object:@"cancel"];
                [[NSNotificationCenter defaultCenter] postNotification:notification];
            }
        }];
    }

    return YES;

我们在AppDelegate中做完以上处理后,iOS 13以前的版本就可以正常回调了。


二、下面就是我们需要的处理iOS 13的回调,iOS 13的回调在支付宝的demo中没有相关的提示。不过,我们去支付宝上搜索可以看到如下的提示(这里需要感谢我同事接入的时候搜索到下面的这个提示):

82230DFA-DEE2-4EC2-9DC8-8786746A4225.png

链接:https://opensupport.alipay.com/support/knowledge/24120/201602049529?ant_source=zsearch

标红的地方就是我们需要的,所以我们只需要根据文档中的提示在SceneDelegate.m中增加文档中增加对应的方法,进行支付回调的处理。代码如下:

- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts
{
    NSArray *members = [URLContexts allObjects];
    UIOpenURLContext *urlContext = [members firstObject];
    
    if ([urlContext.URL.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付,处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:urlContext.URL standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            NSString *payStatus = resultDic[@"resultStatus"];
            if ([payStatus isEqualToString:@"9000"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"success"];
                
            } else if ([payStatus isEqualToString:@"6001"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"cancel"];
                
            } else if ([payStatus isEqualToString:@"8000"] || [payStatus isEqualToString:@"6004"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"confirm"];
            } else {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"fail"];
            }
        }];
        
        [[AlipaySDK defaultService] processAuth_V2Result:urlContext.URL standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            // 解析 auth code
            NSString *result = resultDic[@"result"];
            NSString *payStatus = resultDic[@"resultStatus"];
            NSString *authCode = nil;
            if (result.length>0) {
                NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                for (NSString *subResult in resultArr) {
                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                        authCode = [subResult substringFromIndex:10];
                        break;
                    }
                }
            }
            NSLog(@"授权结果 authCode = %@", authCode?:@"");
            
            if ([payStatus isEqualToString:@"9000"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"success"];
                
            } else if ([payStatus isEqualToString:@"6001"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"cancel"];
                
            } else if ([payStatus isEqualToString:@"8000"] || [payStatus isEqualToString:@"6004"]) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"confirm"];
            } else {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ORDER_SAFEPAY_NOTIFICATION" object:@"fail"];
            }
        }];
    }
    
    // 此处是微信支付
    if ([urlContext.URL.scheme isEqualToString:WXAppId]) {
        [WXApi handleOpenURL:urlContext.URL delegate:self];
    } else {
        // 银联支付回调
        [[UPPaymentControl defaultControl] handlePaymentResult:urlContext.URL completeBlock:^(NSString *code, NSDictionary *data) {
            if ([code isEqualToString:@"success"]) {
                // 结果code为成功时,去商户后台查询一下确保交易是成功的再展示成功
                NSNotification *notification = [NSNotification notificationWithName:@"ORDER_UNIONPAY_NOTIFICATION" object:@"success"];
                [[NSNotificationCenter defaultCenter] postNotification:notification];
                
            } else if ([code isEqualToString:@"fail"]) {
                // 交易失败
                NSNotification *notification = [NSNotification notificationWithName:@"ORDER_UNIONPAY_NOTIFICATION" object:@"fail"];
                [[NSNotificationCenter defaultCenter] postNotification:notification];
            } else if ([code isEqualToString:@"cancel"]) {
                // 交易取消
                NSNotification *notification = [NSNotification notificationWithName:@"ORDER_UNIONPAY_NOTIFICATION" object:@"cancel"];
                [[NSNotificationCenter defaultCenter] postNotification:notification];
            }
        }];
    }
    
}

支付宝集成文档链接:https://docs.open.alipay.com/204/105295/
支付宝支付帮助中心:https://opensupport.alipay.com/support/knowCategory/24120/46864

以上就是本次集成支付宝在iOS 13上的处理。

最后马上就是春节了,大家新春快乐。

推荐阅读更多精彩内容