AppDelegate.mm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #import "AppDelegate.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTBundleURLProvider.h>
  4. #import <React/RCTRootView.h>
  5. #import <React/RCTAppSetupUtils.h>
  6. //#import <RCTJPushModule.h>
  7. #if RCT_NEW_ARCH_ENABLED
  8. #import <React/CoreModulesPlugins.h>
  9. #import <React/RCTCxxBridgeDelegate.h>
  10. #import <React/RCTFabricSurfaceHostingProxyRootView.h>
  11. #import <React/RCTSurfacePresenter.h>
  12. #import <React/RCTSurfacePresenterBridgeAdapter.h>
  13. #import <ReactCommon/RCTTurboModuleManager.h>
  14. #import <react/config/ReactNativeConfig.h>
  15. static NSString *const kRNConcurrentRoot = @"concurrentRoot";
  16. @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> {
  17. RCTTurboModuleManager *_turboModuleManager;
  18. RCTSurfacePresenterBridgeAdapter *_bridgeAdapter;
  19. std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig;
  20. facebook::react::ContextContainer::Shared _contextContainer;
  21. }
  22. @end
  23. #endif
  24. @implementation AppDelegate
  25. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  26. {
  27. // APNS
  28. // JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  29. // if (@available(iOS 12.0, *)) {
  30. // entity.types = JPAuthorizationOptionNone; //JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSou//nd|JPAuthorizationOptionProvidesAppNotificationSettings;
  31. // }
  32. // [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  33. RCTAppSetupPrepareApp(application);
  34. RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];
  35. #if RCT_NEW_ARCH_ENABLED
  36. _contextContainer = std::make_shared<facebook::react::ContextContainer const>();
  37. _reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>();
  38. _contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
  39. _bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
  40. bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
  41. #endif
  42. NSDictionary *initProps = [self prepareInitialProps];
  43. UIView *rootView = [self.reactDelegate createRootViewWithBridge:bridge moduleName:@"taroDemo" initialProperties:initProps];
  44. if (@available(iOS 13.0, *)) {
  45. rootView.backgroundColor = [UIColor systemBackgroundColor];
  46. } else {
  47. rootView.backgroundColor = [UIColor whiteColor];
  48. }
  49. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  50. UIViewController *rootViewController = [self.reactDelegate createRootViewController];
  51. rootViewController.view = rootView;
  52. self.window.rootViewController = rootViewController;
  53. [self.window makeKeyAndVisible];
  54. [super application:application didFinishLaunchingWithOptions:launchOptions];
  55. return YES;
  56. }
  57. /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
  58. ///
  59. /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
  60. /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
  61. /// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
  62. - (BOOL)concurrentRootEnabled
  63. {
  64. // Switch this bool to turn on and off the concurrent root
  65. return true;
  66. }
  67. - (NSDictionary *)prepareInitialProps
  68. {
  69. NSMutableDictionary *initProps = [NSMutableDictionary new];
  70. #ifdef RCT_NEW_ARCH_ENABLED
  71. initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
  72. #endif
  73. return initProps;
  74. }
  75. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  76. {
  77. #if DEBUG
  78. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
  79. #else
  80. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  81. #endif
  82. }
  83. #if RCT_NEW_ARCH_ENABLED
  84. #pragma mark - RCTCxxBridgeDelegate
  85. - (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
  86. {
  87. _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
  88. delegate:self
  89. jsInvoker:bridge.jsCallInvoker];
  90. return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager);
  91. }
  92. #pragma mark RCTTurboModuleManagerDelegate
  93. - (Class)getModuleClassFromName:(const char *)name
  94. {
  95. return RCTCoreModulesClassProvider(name);
  96. }
  97. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  98. jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
  99. {
  100. return nullptr;
  101. }
  102. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  103. initParams:
  104. (const facebook::react::ObjCTurboModule::InitParams &)params
  105. {
  106. return nullptr;
  107. }
  108. - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
  109. {
  110. return RCTAppSetupDefaultModuleFromClass(moduleClass);
  111. }
  112. #endif
  113. //************************************************JPush start************************************************
  114. ////注册 APNS 成功并上报 DeviceToken
  115. //- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  116. // [JPUSHService registerDeviceToken:deviceToken];
  117. //}
  118. //
  119. ////iOS 7 APNS
  120. //- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  121. // // iOS 10 以下 Required
  122. // NSLog(@"iOS 7 APNS");
  123. // [JPUSHService handleRemoteNotification:userInfo];
  124. // [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  125. // completionHandler(UIBackgroundFetchResultNewData);
  126. //}
  127. //
  128. ////iOS 10 前台收到消息
  129. //- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  130. // NSDictionary * userInfo = notification.request.content.userInfo;
  131. // if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  132. // // Apns
  133. // NSLog(@"iOS 10 APNS 前台收到消息");
  134. // [JPUSHService handleRemoteNotification:userInfo];
  135. // [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  136. // }
  137. // else {
  138. // // 本地通知 todo
  139. // NSLog(@"iOS 10 本地通知 前台收到消息");
  140. // [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  141. // }
  142. // //需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  143. // completionHandler(UNNotificationPresentationOptionAlert);
  144. //}
  145. //
  146. ////iOS 10 消息事件回调
  147. //- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
  148. // NSDictionary * userInfo = response.notification.request.content.userInfo;
  149. // if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  150. // // Apns
  151. // NSLog(@"iOS 10 APNS 消息事件回调");
  152. // [JPUSHService handleRemoteNotification:userInfo];
  153. // // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  154. // [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
  155. // [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
  156. // }
  157. // else {
  158. // // 本地通知
  159. // NSLog(@"iOS 10 本地通知 消息事件回调");
  160. // // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  161. // [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
  162. // [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
  163. // }
  164. // // 系统要求执行这个方法
  165. // completionHandler();
  166. //}
  167. //
  168. ////自定义消息
  169. //- (void)networkDidReceiveMessage:(NSNotification *)notification {
  170. // NSDictionary * userInfo = [notification userInfo];
  171. // [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
  172. //}
  173. @end