diff --git a/ios/RCCManager.m b/ios/RCCManager.m index d49b0448dd2..d59deb4e825 100755 --- a/ios/RCCManager.m +++ b/ios/RCCManager.m @@ -158,91 +158,159 @@ -(void)initBridgeWithBundleURL:(NSURL *)bundleURL launchOptions:(NSDictionary *) self.bundleURL = bundleURL; self.sharedBridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; - [self showSplashScreen]; + [[self class] showSplashScreen]; } --(void)showSplashScreen +-(RCTBridge*)getBridge +{ + return self.sharedBridge; +} + +-(UIWindow*)getAppWindow +{ + UIApplication *app = [UIApplication sharedApplication]; + UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0]; + return window; +} + +#pragma mark - Splash Screen + ++ (void)showSplashScreen { + + UIViewController* viewControllerFromLaunchStoryboard; + viewControllerFromLaunchStoryboard = [self viewControllerFromLaunchStoryboard]; + if (viewControllerFromLaunchStoryboard) + { + [self showSplashScreenViewController:viewControllerFromLaunchStoryboard]; + return; + } + CGRect screenBounds = [UIScreen mainScreen].bounds; - UIView *splashView = nil; + UIViewController* viewControllerFromLaunchNib = [self viewControllerFromLaunchNibForScreenBounds:screenBounds]; + if (viewControllerFromLaunchNib) + { + [self showSplashScreenViewController:viewControllerFromLaunchNib]; + return; + } + + UIViewController* viewControllerFromLaunchImage = [self viewControllerFromLaunchImageForScreenBounds:screenBounds]; + if (viewControllerFromLaunchImage) + { + [self showSplashScreenViewController:viewControllerFromLaunchImage]; + return; + } + + UIViewController* viewController = [[UIViewController alloc] init]; + viewController.view.frame = screenBounds; + viewController.view.backgroundColor = [UIColor whiteColor]; + + [self showSplashScreenViewController:viewController]; +} + ++ (UIViewController *)viewControllerFromLaunchStoryboard +{ + NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"]; + if (launchStoryboardName == nil) + { + return nil; + } + + UIStoryboard* storyboard = [UIStoryboard storyboardWithName:launchStoryboardName bundle:nil]; + if (storyboard == nil) + { + return nil; + } + + return storyboard.instantiateInitialViewController; +} + ++ (UIViewController *)viewControllerFromLaunchNibForScreenBounds:(CGRect)screenBounds +{ + NSString* launchStoryboardName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"]; + if (launchStoryboardName == nil) + { + return nil; + } - NSString* launchStoryBoard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"]; - if (launchStoryBoard != nil) - {//load the splash from the storyboard that's defined in the info.plist as the LaunchScreen - @try + @try { + id nibContents = [[NSBundle mainBundle] loadNibNamed:launchStoryboardName owner:self options:nil]; + if (!nibContents || [nibContents count] == 0) { - splashView = [[NSBundle mainBundle] loadNibNamed:launchStoryBoard owner:self options:nil][0]; - if (splashView != nil) - { - splashView.frame = CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height); - } + return nil; } - @catch(NSException *e) + + id firstObject = [nibContents firstObject]; + if (![firstObject isKindOfClass:[UIView class]]) { - splashView = nil; + return nil; } + + UIViewController* viewController = [[UIViewController alloc] init]; + viewController.view = (UIView *)firstObject; + viewController.view.frame = screenBounds; + return viewController; + } + @catch(NSException *exception) { + return nil; } - else - {//load the splash from the DEfault image or from LaunchImage in the xcassets - CGFloat screenHeight = screenBounds.size.height; +} + ++ (UIViewController *)viewControllerFromLaunchImageForScreenBounds:(CGRect)screenBounds +{ + //load the splash from the default image or from LaunchImage in the xcassets + + CGFloat screenHeight = screenBounds.size.height; + + NSString* imageName = @"Default"; + if (screenHeight == 568) + imageName = [imageName stringByAppendingString:@"-568h"]; + else if (screenHeight == 667) + imageName = [imageName stringByAppendingString:@"-667h"]; + else if (screenHeight == 736) + imageName = [imageName stringByAppendingString:@"-736h"]; + + //xcassets LaunchImage files + UIImage *image = [UIImage imageNamed:imageName]; + if (image == nil) + { + imageName = @"LaunchImage"; - NSString* imageName = @"Default"; + if (screenHeight == 480) + imageName = [imageName stringByAppendingString:@"-700"]; if (screenHeight == 568) - imageName = [imageName stringByAppendingString:@"-568h"]; + imageName = [imageName stringByAppendingString:@"-700-568h"]; else if (screenHeight == 667) - imageName = [imageName stringByAppendingString:@"-667h"]; + imageName = [imageName stringByAppendingString:@"-800-667h"]; else if (screenHeight == 736) - imageName = [imageName stringByAppendingString:@"-736h"]; + imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"]; + else if (screenHeight == 812) + imageName = [imageName stringByAppendingString:@"-1100-Portrait-2436h"]; + else if (screenHeight == 1024) + imageName = [imageName stringByAppendingString:@"-Portrait"]; - //xcassets LaunchImage files - UIImage *image = [UIImage imageNamed:imageName]; - if (image == nil) - { - imageName = @"LaunchImage"; - - if (screenHeight == 480) - imageName = [imageName stringByAppendingString:@"-700"]; - if (screenHeight == 568) - imageName = [imageName stringByAppendingString:@"-700-568h"]; - else if (screenHeight == 667) - imageName = [imageName stringByAppendingString:@"-800-667h"]; - else if (screenHeight == 736) - imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"]; - else if (screenHeight == 812) - imageName = [imageName stringByAppendingString:@"-1100-Portrait-2436h"]; - else if (screenHeight == 1024) - imageName = [imageName stringByAppendingString:@"-Portrait"]; - - image = [UIImage imageNamed:imageName]; - } - - if (image != nil) - { - splashView = [[UIImageView alloc] initWithImage:image]; - } + image = [UIImage imageNamed:imageName]; } - if (splashView != nil) + if (image == nil) { - UIViewController *splashVC = [[UIViewController alloc] init]; - splashVC.view = splashView; - - id appDelegate = [UIApplication sharedApplication].delegate; - appDelegate.window.rootViewController = splashVC; - [appDelegate.window makeKeyAndVisible]; + return nil; } + + UIViewController* viewController = [[UIViewController alloc] init]; + + UIImageView* imageView = [[UIImageView alloc] initWithImage:image]; + viewController.view = imageView; + viewController.view.frame = screenBounds; + + return viewController; } --(RCTBridge*)getBridge -{ - return self.sharedBridge; -} - --(UIWindow*)getAppWindow ++ (void)showSplashScreenViewController:(UIViewController *)viewController { - UIApplication *app = [UIApplication sharedApplication]; - UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0]; - return window; + id appDelegate = [UIApplication sharedApplication].delegate; + appDelegate.window.rootViewController = viewController; + [appDelegate.window makeKeyAndVisible]; } -(NSDictionary*)getAppStyle