react-native-navigation的迁移库

RCCManager.m 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #import "RCCManager.h"
  2. #import "RCCViewController.h"
  3. #import <React/RCTBridge.h>
  4. #import <React/RCTRedBox.h>
  5. #import <React/RCTConvert.h>
  6. #import <Foundation/Foundation.h>
  7. @interface RCCManager() <RCTBridgeDelegate>
  8. @property (nonatomic, strong) NSMutableDictionary *modulesRegistry;
  9. @property (nonatomic, strong) RCTBridge *sharedBridge;
  10. @property (nonatomic, strong) NSURL *bundleURL;
  11. @property (nonatomic, strong, readwrite) NSDictionary *globalAppStyle;
  12. @end
  13. @implementation RCCManager
  14. + (instancetype)sharedInstance
  15. {
  16. static RCCManager *sharedInstance = nil;
  17. static dispatch_once_t onceToken = 0;
  18. dispatch_once(&onceToken,^{
  19. if (sharedInstance == nil)
  20. {
  21. sharedInstance = [[RCCManager alloc] init];
  22. }
  23. });
  24. return sharedInstance;
  25. }
  26. + (instancetype)sharedIntance
  27. {
  28. return [RCCManager sharedInstance];
  29. }
  30. - (instancetype)init
  31. {
  32. self = [super init];
  33. if (self)
  34. {
  35. self.modulesRegistry = [@{} mutableCopy];
  36. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTReloadNotification object:nil];
  37. }
  38. return self;
  39. }
  40. - (NSDictionary *)getLaunchArgs {
  41. NSMutableDictionary* mutableArguments = [[NSMutableDictionary alloc] init];
  42. NSArray* arguments = [[NSProcessInfo processInfo] arguments];
  43. for (int i = 0; i < [arguments count]; i+=2) {
  44. NSString* key = [arguments objectAtIndex:i];
  45. NSString* value = [arguments objectAtIndex:i+1];
  46. [mutableArguments setObject:value forKey:key];
  47. }
  48. return mutableArguments;
  49. }
  50. -(void)clearModuleRegistry
  51. {
  52. [self.modulesRegistry removeAllObjects];
  53. }
  54. -(void)onRNReload
  55. {
  56. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  57. appDelegate.window.rootViewController = nil;
  58. [self setAppStyle:nil];
  59. [self clearModuleRegistry];
  60. }
  61. -(void)registerController:(UIViewController*)controller componentId:(NSString*)componentId componentType:(NSString*)componentType
  62. {
  63. if (controller == nil || componentId == nil)
  64. {
  65. return;
  66. }
  67. NSMutableDictionary *componentsDic = self.modulesRegistry[componentType];
  68. if (componentsDic == nil)
  69. {
  70. componentsDic = [@{} mutableCopy];
  71. self.modulesRegistry[componentType] = componentsDic;
  72. }
  73. /*
  74. TODO: we really want this error, but we need to unregister controllers when they dealloc
  75. if (componentsDic[componentId])
  76. {
  77. [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]];
  78. }
  79. */
  80. componentsDic[componentId] = controller;
  81. }
  82. -(void)unregisterController:(UIViewController*)vc
  83. {
  84. if (vc == nil) return;
  85. for (NSString *key in [self.modulesRegistry allKeys])
  86. {
  87. NSMutableDictionary *componentsDic = self.modulesRegistry[key];
  88. for (NSString *componentID in [componentsDic allKeys])
  89. {
  90. UIViewController *tmpVc = componentsDic[componentID];
  91. if (tmpVc == vc)
  92. {
  93. [componentsDic removeObjectForKey:componentID];
  94. }
  95. }
  96. }
  97. }
  98. -(id)getControllerWithId:(NSString*)componentId componentType:(NSString*)componentType
  99. {
  100. if (componentId == nil)
  101. {
  102. return nil;
  103. }
  104. id component = nil;
  105. NSMutableDictionary *componentsDic = self.modulesRegistry[componentType];
  106. if (componentsDic != nil)
  107. {
  108. component = componentsDic[componentId];
  109. }
  110. return component;
  111. }
  112. -(NSString*) getIdForController:(UIViewController*)vc
  113. {
  114. if([vc isKindOfClass:[RCCViewController class]])
  115. {
  116. NSString *controllerId = ((RCCViewController*)vc).controllerId;
  117. if(controllerId != nil)
  118. {
  119. return controllerId;
  120. }
  121. }
  122. for (NSString *key in [self.modulesRegistry allKeys])
  123. {
  124. NSMutableDictionary *componentsDic = self.modulesRegistry[key];
  125. for (NSString *componentID in [componentsDic allKeys])
  126. {
  127. UIViewController *tmpVc = componentsDic[componentID];
  128. if (tmpVc == vc)
  129. {
  130. return componentID;
  131. }
  132. }
  133. }
  134. return nil;
  135. }
  136. -(void)initBridgeWithBundleURL:(NSURL *)bundleURL
  137. {
  138. [self initBridgeWithBundleURL :bundleURL launchOptions:nil];
  139. }
  140. -(void)initBridgeWithBundleURL:(NSURL *)bundleURL launchOptions:(NSDictionary *)launchOptions
  141. {
  142. if (self.sharedBridge) return;
  143. self.bundleURL = bundleURL;
  144. self.sharedBridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  145. [[self class] showSplashScreen];
  146. }
  147. -(RCTBridge*)getBridge
  148. {
  149. return self.sharedBridge;
  150. }
  151. -(UIWindow*)getAppWindow
  152. {
  153. UIApplication *app = [UIApplication sharedApplication];
  154. UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0];
  155. return window;
  156. }
  157. #pragma mark - Splash Screen
  158. + (void)showSplashScreen
  159. {
  160. UIViewController* viewControllerFromLaunchStoryboard;
  161. viewControllerFromLaunchStoryboard = [self viewControllerFromLaunchStoryboard];
  162. if (viewControllerFromLaunchStoryboard)
  163. {
  164. [self showSplashScreenViewController:viewControllerFromLaunchStoryboard];
  165. return;
  166. }
  167. CGRect screenBounds = [UIScreen mainScreen].bounds;
  168. UIViewController* viewControllerFromLaunchNib = [self viewControllerFromLaunchNibForScreenBounds:screenBounds];
  169. if (viewControllerFromLaunchNib)
  170. {
  171. [self showSplashScreenViewController:viewControllerFromLaunchNib];
  172. return;
  173. }
  174. UIViewController* viewControllerFromLaunchImage = [self viewControllerFromLaunchImageForScreenBounds:screenBounds];
  175. if (viewControllerFromLaunchImage)
  176. {
  177. [self showSplashScreenViewController:viewControllerFromLaunchImage];
  178. return;
  179. }
  180. UIViewController* viewController = [[UIViewController alloc] init];
  181. viewController.view.frame = screenBounds;
  182. viewController.view.backgroundColor = [UIColor whiteColor];
  183. [self showSplashScreenViewController:viewController];
  184. }
  185. + (UIViewController *)viewControllerFromLaunchStoryboard
  186. {
  187. NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
  188. if (launchStoryboardName == nil)
  189. {
  190. return nil;
  191. }
  192. @try {
  193. UIStoryboard* storyboard = [UIStoryboard storyboardWithName:launchStoryboardName bundle:nil];
  194. return storyboard.instantiateInitialViewController;
  195. }
  196. @catch(NSException *exception) {
  197. return nil;
  198. }
  199. }
  200. + (UIViewController *)viewControllerFromLaunchNibForScreenBounds:(CGRect)screenBounds
  201. {
  202. NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
  203. if (launchStoryboardName == nil)
  204. {
  205. return nil;
  206. }
  207. @try {
  208. id nibContents = [[NSBundle mainBundle] loadNibNamed:launchStoryboardName owner:self options:nil];
  209. if (!nibContents || [nibContents count] == 0)
  210. {
  211. return nil;
  212. }
  213. id firstObject = [nibContents firstObject];
  214. if (![firstObject isKindOfClass:[UIView class]])
  215. {
  216. return nil;
  217. }
  218. UIViewController* viewController = [[UIViewController alloc] init];
  219. viewController.view = (UIView *)firstObject;
  220. viewController.view.frame = screenBounds;
  221. return viewController;
  222. }
  223. @catch(NSException *exception) {
  224. return nil;
  225. }
  226. }
  227. + (UIViewController *)viewControllerFromLaunchImageForScreenBounds:(CGRect)screenBounds
  228. {
  229. //load the splash from the default image or from LaunchImage in the xcassets
  230. CGFloat screenHeight = screenBounds.size.height;
  231. NSString* imageName = @"Default";
  232. if (screenHeight == 568)
  233. imageName = [imageName stringByAppendingString:@"-568h"];
  234. else if (screenHeight == 667)
  235. imageName = [imageName stringByAppendingString:@"-667h"];
  236. else if (screenHeight == 736)
  237. imageName = [imageName stringByAppendingString:@"-736h"];
  238. //xcassets LaunchImage files
  239. UIImage *image = [UIImage imageNamed:imageName];
  240. if (image == nil)
  241. {
  242. imageName = @"LaunchImage";
  243. if (screenHeight == 480)
  244. imageName = [imageName stringByAppendingString:@"-700"];
  245. if (screenHeight == 568)
  246. imageName = [imageName stringByAppendingString:@"-700-568h"];
  247. else if (screenHeight == 667)
  248. imageName = [imageName stringByAppendingString:@"-800-667h"];
  249. else if (screenHeight == 736)
  250. imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
  251. else if (screenHeight == 812)
  252. imageName = [imageName stringByAppendingString:@"-1100-Portrait-2436h"];
  253. else if (screenHeight == 1024)
  254. imageName = [imageName stringByAppendingString:@"-Portrait"];
  255. image = [UIImage imageNamed:imageName];
  256. }
  257. if (image == nil)
  258. {
  259. return nil;
  260. }
  261. UIViewController* viewController = [[UIViewController alloc] init];
  262. UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
  263. viewController.view = imageView;
  264. viewController.view.frame = screenBounds;
  265. return viewController;
  266. }
  267. + (void)showSplashScreenViewController:(UIViewController *)viewController
  268. {
  269. id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
  270. appDelegate.window.rootViewController = viewController;
  271. [appDelegate.window makeKeyAndVisible];
  272. }
  273. -(NSDictionary*)getAppStyle
  274. {
  275. return [NSDictionary dictionaryWithDictionary:self.globalAppStyle];
  276. }
  277. -(void)setAppStyle:(NSDictionary*)appStyle
  278. {
  279. self.globalAppStyle = [NSDictionary dictionaryWithDictionary:appStyle];
  280. [self applyAppStyle];
  281. }
  282. -(void)applyAppStyle {
  283. id backButtonImage = self.globalAppStyle[@"backButtonImage"];
  284. UIImage *image = backButtonImage ? [RCTConvert UIImage:backButtonImage] : nil;
  285. [[UINavigationBar appearance] setBackIndicatorImage:image];
  286. [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:image];
  287. }
  288. #pragma mark - RCTBridgeDelegate methods
  289. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  290. {
  291. return self.bundleURL;
  292. }
  293. @end