React Native 0.66.4升0.71.3:AppDelegate适配问题咨询
解决React Native 0.71.3升级中的AppDelegate配置问题
1. 在修改后的AppDelegate.h中添加UNUserNotificationCenterDelegate
修改后的AppDelegate.h需先导入推送相关头文件,再在类声明中追加协议,具体代码如下:
#import <RCTAppDelegate.h> #import <UIKit/UIKit.h> #import <UserNotifications/UserNotifications.h> @interface AppDelegate : RCTAppDelegate <UNUserNotificationCenterDelegate> @end
完成后你的AppDelegate就能正常处理推送相关的代理逻辑了。
2. 迁移原AppDelegate.m的模块与代码到新的AppDelegate.mm
按以下步骤操作即可:
- 删除旧的
AppDelegate.m文件,新建AppDelegate.mm文件 - 将原
AppDelegate.m中所有#import导入语句复制到新文件顶部 - 把原文件里的实现代码(比如
didFinishLaunchingWithOptions初始化方法、推送代理方法等)全部迁移到AppDelegate.mm中
举个迁移后的代码示例:
#import "AppDelegate.h" #import <React/RCTRootView.h> // 这里添加原m文件里的其他第三方库或模块导入语句 @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 保留原有初始化逻辑,也可根据RN 0.71的要求做微调 [super application:application didFinishLaunchingWithOptions:launchOptions]; // 你的自定义初始化代码 return YES; } // 保留原有的推送代理方法 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { // 原有的推送响应处理逻辑 completionHandler(); } @end
由于.mm是Objective-C++文件,完全兼容原Objective-C代码,直接迁移即可,无需修改原有业务逻辑(除非RN新版本有特定API变更)。
内容的提问来源于stack exchange,提问作者Sachin Chandiramani
相关产品推荐
相关产品推荐

