Skip to content

Instantly share code, notes, and snippets.

@mattotodd
Forked from Jpoliachik/AppDelegate.m
Created July 4, 2018 02:58
Show Gist options
  • Select an option

  • Save mattotodd/643e8b4b4a1305c4db8149fe666178ad to your computer and use it in GitHub Desktop.

Select an option

Save mattotodd/643e8b4b4a1305c4db8149fe666178ad to your computer and use it in GitHub Desktop.

Revisions

  1. @Jpoliachik Jpoliachik created this gist Dec 19, 2016.
    39 changes: 39 additions & 0 deletions AppDelegate.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 1. init window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIViewController *rootViewController = [UIViewController new];

    // 2. backgroundView using LaunchScreen.xib
    UIView *backgroundView = [[[NSBundle mainBundle] loadNibNamed:@"LaunchScreen" owner:self options:nil] firstObject];
    backgroundView.frame = self.window.bounds;

    // 3. ReactNative init
    NSURL *jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
    moduleName:@"MyAwesomeApp"
    initialProperties:nil
    launchOptions:launchOptions];

    // 4. rootView background clear, so our backgroundView can show until ReactNative has fully loaded
    rootView.backgroundColor = [UIColor clearColor];

    // 5. set loadingView to the LaunchScreen.xib view too
    // explicitly set all frames
    UIView *launchScreenView = [[[NSBundle mainBundle] loadNibNamed:@"LaunchScreen" owner:self options:nil] firstObject];
    rootView.frame = self.window.bounds;
    launchScreenView.frame = self.window.bounds;
    rootView.loadingView = launchScreenView;

    // 6. set the backgroundView as main view for the rootViewController (instead of the rootView)
    rootViewController.view = backgroundView;

    // 7. show
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];

    // 8. after the window is visible, add the rootView as a subview to your backgroundView
    [backgroundView addSubview:rootView];

    return YES;
    }