react-native-navigation的迁移库

RCCManager.m 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #import "RCCManager.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTRedBox.h>
  4. #import <Foundation/Foundation.h>
  5. @interface RCCManager() <RCTBridgeDelegate>
  6. @property (nonatomic, strong) NSMutableDictionary *modulesRegistry;
  7. @property (nonatomic, strong) RCTBridge *sharedBridge;
  8. @property (nonatomic, strong) NSURL *bundleURL;
  9. @end
  10. @implementation RCCManager
  11. + (instancetype)sharedInstance
  12. {
  13. static RCCManager *sharedInstance = nil;
  14. static dispatch_once_t onceToken = 0;
  15. dispatch_once(&onceToken,^{
  16. if (sharedInstance == nil)
  17. {
  18. sharedInstance = [[RCCManager alloc] init];
  19. }
  20. });
  21. return sharedInstance;
  22. }
  23. + (instancetype)sharedIntance
  24. {
  25. return [RCCManager sharedInstance];
  26. }
  27. - (instancetype)init
  28. {
  29. self = [super init];
  30. if (self)
  31. {
  32. self.modulesRegistry = [@{} mutableCopy];
  33. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTReloadNotification object:nil];
  34. }
  35. return self;
  36. }
  37. -(void)clearModuleRegistry
  38. {
  39. [self.modulesRegistry removeAllObjects];
  40. }
  41. -(void)onRNReload
  42. {
  43. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  44. appDelegate.window.rootViewController = nil;
  45. [self clearModuleRegistry];
  46. }
  47. -(void)registerController:(UIViewController*)controller componentId:(NSString*)componentId componentType:(NSString*)componentType
  48. {
  49. if (controller == nil || componentId == nil)
  50. {
  51. return;
  52. }
  53. NSMutableDictionary *componentsDic = self.modulesRegistry[componentType];
  54. if (componentsDic == nil)
  55. {
  56. componentsDic = [@{} mutableCopy];
  57. self.modulesRegistry[componentType] = componentsDic;
  58. }
  59. /*
  60. TODO: we really want this error, but we need to unregister controllers when they dealloc
  61. if (componentsDic[componentId])
  62. {
  63. [self.sharedBridge.redBox showErrorMessage:[NSString stringWithFormat:@"Controllers: controller with id %@ is already registered. Make sure all of the controller id's you use are unique.", componentId]];
  64. }
  65. */
  66. componentsDic[componentId] = controller;
  67. }
  68. -(void)unregisterController:(UIViewController*)vc
  69. {
  70. if (vc == nil) return;
  71. for (NSString *key in [self.modulesRegistry allKeys])
  72. {
  73. NSMutableDictionary *componentsDic = self.modulesRegistry[key];
  74. for (NSString *componentID in [componentsDic allKeys])
  75. {
  76. UIViewController *tmpVc = componentsDic[componentID];
  77. if (tmpVc == vc)
  78. {
  79. [componentsDic removeObjectForKey:componentID];
  80. }
  81. }
  82. }
  83. }
  84. -(id)getControllerWithId:(NSString*)componentId componentType:(NSString*)componentType
  85. {
  86. if (componentId == nil)
  87. {
  88. return nil;
  89. }
  90. id component = nil;
  91. NSMutableDictionary *componentsDic = self.modulesRegistry[componentType];
  92. if (componentsDic != nil)
  93. {
  94. component = componentsDic[componentId];
  95. }
  96. return component;
  97. }
  98. -(void)initBridgeWithBundleURL:(NSURL *)bundleURL
  99. {
  100. [self initBridgeWithBundleURL :bundleURL launchOptions:nil];
  101. }
  102. -(void)initBridgeWithBundleURL:(NSURL *)bundleURL launchOptions:(NSDictionary *)launchOptions
  103. {
  104. if (self.sharedBridge) return;
  105. self.bundleURL = bundleURL;
  106. self.sharedBridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  107. [self showSplashScreen];
  108. }
  109. -(void)showSplashScreen
  110. {
  111. CGRect screenBounds = [UIScreen mainScreen].bounds;
  112. UIViewController *splashViewController = nil;
  113. NSString* launchStoryBoard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
  114. if (launchStoryBoard != nil)
  115. {//load the splash from the storyboard that's defined in the info.plist as the LaunchScreen
  116. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:launchStoryBoard bundle:[NSBundle mainBundle]];
  117. splashViewController = [storyboard instantiateInitialViewController];
  118. }
  119. else
  120. {//load the splash from the DEfault image or from LaunchImage in the xcassets
  121. CGFloat screenHeight = screenBounds.size.height;
  122. NSString* imageName = @"Default";
  123. if (screenHeight == 568)
  124. imageName = [imageName stringByAppendingString:@"-568h"];
  125. else if (screenHeight == 667)
  126. imageName = [imageName stringByAppendingString:@"-667h"];
  127. else if (screenHeight == 736)
  128. imageName = [imageName stringByAppendingString:@"-736h"];
  129. //xcassets LaunchImage files
  130. UIImage *image = [UIImage imageNamed:imageName];
  131. if (image == nil)
  132. {
  133. imageName = @"LaunchImage";
  134. if (screenHeight == 480)
  135. imageName = [imageName stringByAppendingString:@"-700"];
  136. if (screenHeight == 568)
  137. imageName = [imageName stringByAppendingString:@"-700-568h"];
  138. else if (screenHeight == 667)
  139. imageName = [imageName stringByAppendingString:@"-800-667h"];
  140. else if (screenHeight == 736)
  141. imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
  142. image = [UIImage imageNamed:imageName];
  143. }
  144. if (image != nil)
  145. {
  146. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  147. splashViewController = [[UIViewController alloc] init];
  148. splashViewController.view = imageView;
  149. }
  150. }
  151. if (splashViewController != nil) {
  152. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  153. appDelegate.window.rootViewController = splashViewController;
  154. [appDelegate.window makeKeyAndVisible];
  155. }
  156. }
  157. -(RCTBridge*)getBridge
  158. {
  159. return self.sharedBridge;
  160. }
  161. -(UIWindow*)getAppWindow
  162. {
  163. UIApplication *app = [UIApplication sharedApplication];
  164. UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0];
  165. return window;
  166. }
  167. #pragma mark - RCTBridgeDelegate methods
  168. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  169. {
  170. return self.bundleURL;
  171. }
  172. @end