react-native-navigation的迁移库

RCCManager.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #import "RCCManager.h"
  2. #import "RCTBridge.h"
  3. #import "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. UIView *splashView = 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. @try
  117. {
  118. splashView = [[NSBundle mainBundle] loadNibNamed:launchStoryBoard owner:self options:nil][0];
  119. if (splashView != nil)
  120. {
  121. splashView.frame = CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height);
  122. }
  123. }
  124. @catch(NSException *e)
  125. {
  126. splashView = nil;
  127. }
  128. }
  129. else
  130. {//load the splash from the DEfault image or from LaunchImage in the xcassets
  131. CGFloat screenHeight = screenBounds.size.height;
  132. NSString* imageName = @"Default";
  133. if (screenHeight == 568)
  134. imageName = [imageName stringByAppendingString:@"-568h"];
  135. else if (screenHeight == 667)
  136. imageName = [imageName stringByAppendingString:@"-667h"];
  137. else if (screenHeight == 736)
  138. imageName = [imageName stringByAppendingString:@"-736h"];
  139. //xcassets LaunchImage files
  140. UIImage *image = [UIImage imageNamed:imageName];
  141. if (image == nil)
  142. {
  143. imageName = @"LaunchImage";
  144. if (screenHeight == 480)
  145. imageName = [imageName stringByAppendingString:@"-700"];
  146. if (screenHeight == 568)
  147. imageName = [imageName stringByAppendingString:@"-700-568h"];
  148. else if (screenHeight == 667)
  149. imageName = [imageName stringByAppendingString:@"-800-667h"];
  150. else if (screenHeight == 736)
  151. imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
  152. image = [UIImage imageNamed:imageName];
  153. }
  154. if (image != nil)
  155. {
  156. splashView = [[UIImageView alloc] initWithImage:image];
  157. }
  158. }
  159. if (splashView != nil)
  160. {
  161. UIViewController *splashVC = [[UIViewController alloc] init];
  162. splashVC.view = splashView;
  163. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  164. appDelegate.window.rootViewController = splashVC;
  165. [appDelegate.window makeKeyAndVisible];
  166. }
  167. }
  168. -(RCTBridge*)getBridge
  169. {
  170. return self.sharedBridge;
  171. }
  172. -(UIWindow*)getAppWindow
  173. {
  174. UIApplication *app = [UIApplication sharedApplication];
  175. UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0];
  176. return window;
  177. }
  178. #pragma mark - RCTBridgeDelegate methods
  179. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  180. {
  181. return self.bundleURL;
  182. }
  183. @end