美文网首页
iOS13 支付宝/微信 支付成功没有回调

iOS13 支付宝/微信 支付成功没有回调

作者: 是西一啊 | 来源:发表于2020-04-16 17:29 被阅读0次

老项目在iOS13以上的手机上跑,支付成功后发现没有走回调(在iOS13以下的版本正常)。查资料发现是因为iOS13的这些支付回调都在SceneDelegate中。SceneDelegate是Xcode11创建项目后自动生成的,负责AppDelegate的某些功能、iPadOS的多窗口功能,这里不作详细介绍。

为了弄SceneDelegate中的支付回调,先做好准备工作:
1.项目的info.list里加上新的key value(也可以新建一个项目,把新项目里的直接复制过来),如图。


info.png

2.AppDelegate.m里的变化

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch

    if (@available(iOS 13.0, *)){
        NSLog(@"========iOS13========");
    }else {
        [self baseSetting]; //加载设置一些第三方库 支付宝、微信、定位等等
        [self initBaseController]; //创建window、导航栏等
    }
    return YES;
}

#pragma mark - UISceneSession lifecycle

- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options  API_AVAILABLE(ios(13.0)){
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    if (@available(iOS 13.0, *)){
        return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
    }else {
        return nil;
    }
}

- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions  API_AVAILABLE(ios(13.0)){
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

3.SceneDelegate.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (strong, nonatomic) UIWindow * window;

@end

NS_ASSUME_NONNULL_END

4.SceneDelegate.m .m里创建window的方式发生了变化,这里要注意一下

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions  API_AVAILABLE(ios(13.0)){
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
    if (@available(iOS 13.0, *)){
        [self baseSetting]; //加载设置一些第三方库 支付宝、微信、定位等等
        [self initBaseControllerWithScene:(UIWindowScene *)scene andWithSession:session options:connectionOptions]; //创建window、导航栏等
    }
}

-(void)initBaseControllerWithScene:(UIWindowScene *)scene andWithSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions
API_AVAILABLE(ios(13.0)){
    
    self.window = [[UIWindow alloc] initWithWindowScene:scene];
    self.window.frame = scene.coordinateSpace.bounds;
    self.window.backgroundColor = WHITE_Color;
    [self.window makeKeyAndVisible];
    //创建导航栏什么的
    //......  self.window.rootViewController = ;
}

- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts
API_AVAILABLE(ios(13.0))
{
    NSEnumerator *enumerator = [URLContexts objectEnumerator];
    
    UIOpenURLContext *context;

    while (context = [enumerator nextObject]) {
        
        NSURL *url = context.URL;
        
        if([[NSString stringWithFormat:@"%@",url] rangeOfString:@"Wechat_Login"].location != NSNotFound)
        { //微信登录
            //#define appDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
            vc //login登录页面
            [WXApi handleOpenURL:url delegate:vc];
        }else if(([url.host isEqualToString:@"pay"] || [url.host isEqualToString:@"resendContextReqByScheme"] || [url.host isEqualToString:@"platformId=wechat"]) && [url.scheme isEqualToString:@"你的微信appid:wx************"]){
            //"微信支付分授权和微信支付等回调====>我写在WXPayService.m的delegate方法中了,可以自己封装一下"
            [WXApi handleOpenURL:url delegate:[WXPayService sharedInstance]];
        }else if ([url.host isEqualToString:@"safepay"]) { //支付宝登录和支付
            NSString *scope = [NSString getParamByName:@"scope" URLString:[NSString stringWithFormat:@"%@",url]];
            if ([scope isEqualToString:@"kuaijie"]) { //支付宝快捷登录
                [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:nil];
            }else {
                //跳转支付宝钱包进行支付,处理支付结果
                [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:nil];
            }
        }
    }
}

NSString的Category

#pragma mark - ==========获取url中的参数==========
+ (NSString *)getParamByName:(NSString *)name URLString:(NSString *)url
{
    NSError *error;
    NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)", name];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    
    // 执行匹配的过程
    NSArray *matches = [regex matchesInString:url
                                      options:0
                                        range:NSMakeRange(0, [url length])];
    for (NSTextCheckingResult *match in matches) {
        NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]];  // 分组2所对应的串
        return tagValue;
    }
    return @"";
}

这下就能收到支付成功/失败的回调了,有问题或者我写的哪里有问题欢迎留言指正。

相关文章

网友评论

      本文标题:iOS13 支付宝/微信 支付成功没有回调

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