react-native-navigation的迁移库

RCCManager.m 9.2KB

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