美文网首页iOS Developer
支付宝支付完成以后跳转回app

支付宝支付完成以后跳转回app

作者: Billy_W | 来源:发表于2017-02-13 18:39 被阅读0次

    1.支付宝支付以后需要跳转到自己的app,需要在自己项目AppDelegate.m里面 添加如下方法:

    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation
    {
        if ([url.host isEqualToString:@"safepay"]) {
            // 支付跳转支付宝钱包进行支付,处理支付结果
            [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
            }];
    
            // 授权跳转支付宝钱包进行支付,处理支付结果
            [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
                // 解析 auth code
                NSString *result = resultDic[@"result"];
                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?:@"");
            }];
        }
        return YES;
    }
    

    iOS 9之后使用

    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
    {
        if ([url.host isEqualToString:@"safepay"]) {
            // 支付跳转支付宝钱包进行支付,处理支付结果
            [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
            }];
    
            // 授权跳转支付宝钱包进行支付,处理支付结果
            [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
                // 解析 auth code
                NSString *result = resultDic[@"result"];
                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?:@"");
            }];
        }
        return YES;
    }
    

    2.如果还是无法返回自己的app,请检查支付宝使用时

    [[AlipaySDK defaultService] payOrder: fromScheme: callback:^(NSDictionary *resultDic) {
    NSLog(@"%@",result)}];
    

    代码附近

    NSString *appScheme = @"自己app名称";
    [AlixLibService payOrder:orderString AndScheme:appScheme seletor:_result target:self];
    

    我把这里写成自己项目的名称,再次运行,发现支付后还是无法返回。原来是需要在info.plist文件中添加一个键值,如下图:


    app名称添加位置.jpeg

    这样之后就可以在调用支付宝之后返回自己的app了。

    相关文章

      网友评论

        本文标题:支付宝支付完成以后跳转回app

        本文链接:https://www.haomeiwen.com/subject/onjtwttx.html