iOS 13 Xcode 11.3 微信SDK版本 1.8.6 关于微信登录和微信支付的回调

创建项目时使用的Xcode 版本是 11.3 , AppDelegate 已拆分开来 ,存在ScaneDelegate 窗口代理 , 微信SDK也更新到最新版本, 1.8.6, 如果Xcode 的版本低于11, 在AppDelegate 没有被拆分的时候创建的项目 . 使用微信登录或支付在 iOS 系统版本 13 以上都是OK 的 , 需要讲的是高版本Xcode下的微信回调,

窗口代理SceneDelegate'


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions  API_AVAILABLE(ios(13.0)){
    
    if (scene) {
        
       [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        
       UIWindowScene *windowScene = (UIWindowScene *)scene;
       
       self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
        
       self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        
        [NSThread sleepForTimeInterval:1.5];
 //    已废弃,  使用 [WXApi registerApp:APP_ID universalLink:Universal_Links];
//       [WXApi registerApp:APP_ID];
//     Universal_Links  是通用配置,  微信SDK 有详细说明,
        [WXApi registerApp:APP_ID universalLink:Universal_Links];
        
      //  其他界面操作
        LoginViewController *loginVC = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];

        self.window.rootViewController = loginVC;

        self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
        
        [self.window makeKeyAndVisible];
        
       }
}

在调试过程中. 发现 iOS 版本在 13 至13.2 时回调的方法如下:

#pragma mark ===== 适配 iOS 13 - 13.2
-(void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts
{
       NSEnumerator *enumerator = [URLContexts objectEnumerator];
    
       UIOpenURLContext *context;
    
       while (context = [enumerator nextObject]) {
           
           NSString *url = [NSString stringWithFormat:@"%@",context.URL];
           //    getURLParameters  是 自定义的一个字符串转字典的方法, 
           NSDictionary *urlDic = [self getURLParameters:url];
           
           if([url containsString:@"oauth"]){
               
               if (urlDic.allKeys > 0) {
                   //  微信登录时的状态标识
                   if ([urlDic[@"state"] isEqualToString:@"YYLxxxx"]) {
                       
                       NSString *requestStr = [NSString stringWithFormat:@"code=%@&type=1",urlDic[@"code"]];
                      // 自己封装的方法向后台请求微信登录
                       [self wechatLoginWithUrlStr:requestStr];
                       
                      //    微信回调的状态等于 AuthorizeApp 授权认证时
                   }else if ([urlDic[@"state"] isEqualToString:@"AuthorizeApp"]){
                       
                       NSDictionary *dic = @{@"code":urlDic[@"code"]};
                       
                       NOTIFICATION_POST_USERINFO(WXCHATAUTHORIZE_KEY, nil, dic);
                   }
               }
                    // 微信支付的回调
           }else if([url containsString:@"pay"]){
               
               switch([urlDic[@"ret"] intValue]){
                        case 0:
                   {
                         NSLog(@"支付成功");
                       NOTIFICATION_POST(PAYSUCCESS_KEY);
                   }
                       break;
                        case -2:
                   {
                       NSLog(@"用户点击取消");
                       NOTIFICATION_POST(PAYCANCEL_KAY);
                   }
                        break;
                   default: NOTIFICATION_POST(PAYFAILE_KEY);
                            NSLog(@"支付失败,retcode = %@",urlDic[@"ret"]);
                         break;
               }
              //    重点:  判断APPID 是否一致 , 调用微信Api处理.  
           }else if ([context.URL.scheme rangeOfString:@"wx56102e6e4cxxxxxxx"].length!=0) {
                   
                   // 完整的 URL样式应为:  wx56102e4c3rr6hfd://resendContextReqByScheme?wechat_auth_context_id=9xxxxxx6228692e3957321bc9xxxxxxxxxxxx
                   [WXApi handleOpenURL:context.URL delegate:self];
            }
            //  打印出相关的数据 
           NSLog(@"urlDic ====  %@",urlDic);
           NSLog(@"context.URL =====%@",context.URL);
           NSLog(@"context.options.sourceApplication ===== %@",context.options.sourceApplication);
       }
}

以上在我测试时. 系统版本 13 至 13.2 回调正常, 在 iOS 13.3 系统版本上依然没有回调 , 在写项目时 , 13.3 刚出来一个星期, 以为万事大吉, 万万没想到..hhhhhh
13.3 的回调方法:

#pragma mark ====== 适配 iOS 13.3

- (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity
{
    
    if (userActivity.webpageURL) {
        NSLog(@" %@",userActivity.webpageURL);
       BOOL isSuccess = [WXApi handleOpenUniversalLink:userActivity delegate:self];
        NSLog(@"ios 13 %@",@(isSuccess));
    }
}

到此就全部ok了 , 至于

-(void)onResp:(BaseResp *)resp

这个方法, 我也有写上, 和旧版写在 Appdelegate 一致, 但在测试过程中, 好像并没有走这个方法,

附上字符串 转字典的封装方法:


-(NSMutableDictionary*)getURLParameters:(NSString *)urlStr {

        NSRange range = [urlStr rangeOfString:@"?"];

        if(range.location==NSNotFound) {

        return nil;

        }

        NSMutableDictionary *params = [NSMutableDictionary dictionary];

        NSString *parametersString = [urlStr substringFromIndex:range.location+1];

        if([parametersString containsString:@"&"]) {

        NSArray *urlComponents = [parametersString  componentsSeparatedByString:@"&"];

        for(NSString *keyValuePair in urlComponents) {

        //生成key/value

        NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];

        NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];

        NSString*value = [pairComponents.lastObject stringByRemovingPercentEncoding];

        //key不能为nil

        if(key==nil|| value ==nil) {

        continue;

        }

        id existValue = [params valueForKey:key];

        if(existValue !=nil) {

        //已存在的值,生成数组。

        if([existValue isKindOfClass:[NSArray class]]) {

        //已存在的值生成数组

        NSMutableArray*items = [NSMutableArray arrayWithArray:existValue];

        [items addObject:value];

        [params setValue:items forKey:key];

        }else{

        //非数组

        [params setValue:@[existValue,value]forKey:key];

        }

        }else{

        //设置值

        [params setValue:value forKey:key];

        }

        }

        }else{

        //单个参数生成key/value

        NSArray *pairComponents = [parametersString componentsSeparatedByString:@"="];

        if(pairComponents.count==1) {

        return nil;

        }

        //分隔值

        NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];

        NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding];

        //key不能为nil

        if(key ==nil|| value ==nil) {

        return nil;

        }

        //设置值

        [params setValue:value forKey:key];

        }

        return params;

}

你可能感兴趣的:(iOS 13 Xcode 11.3 微信SDK版本 1.8.6 关于微信登录和微信支付的回调)