React-Native iOS - How to show a view controller after Launch screen, before initialising react native bridge (iOS)

五迷三道 提交于 2020-04-30 11:44:43

问题


I have an animation (splash screen animation) that is in a native iOS view controller(swift), and I want to show that animation's view vontroller after the launch screen for 3 seconds before launching the react-native bridge.

I tried to play around with the appdelegate.m file, here's what I came up with😩.

RN version 0.61

#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"AnimationView" bundle:[NSBundle mainBundle]];
     UIViewController *vc =[storyboard instantiateInitialViewController];


  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"SplashScreenFinal"
                                            initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     self.window.rootViewController = vc;
     [self.window makeKeyAndVisible];


  if (after 3 secs) {
      self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
      UIViewController *rootViewController = [UIViewController new];
      rootViewController.view = rootView;
      self.window.rootViewController = rootViewController;
      [self.window makeKeyAndVisible];

  }
  return YES;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end

Thanks.


回答1:


  • You call method [storyboard instantiateInitialViewController] this method will call create ViewController that you set default for this storyboard. Have you check Is Initial View Controller for the first ViewController you want display?

  • You can using another way to init view controller [storyboard instantiateViewControllerWithIdentifier:@"myAnimationViewController"]; remember set Storyboard ID myAnimationViewController for view controller you want to set rootViewController.

Code example:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"AnimationViewStoryboard" bundle:[NSBundle mainBundle]];
  // this code just run when storyboard have default ViewController
//  UIViewController *vc =[storyboard instantiateInitialViewController];

  UIViewController *vc =[storyboard         instantiateViewControllerWithIdentifier:@"myAnimationViewController"];

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                               moduleName:@"DemoReactNative"
                                        initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;

  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  self.window.rootViewController = vc;
  [self.window makeKeyAndVisible];

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    self.window.rootViewController = rootViewController;
  });

  return YES;
}


来源:https://stackoverflow.com/questions/60296229/react-native-ios-how-to-show-a-view-controller-after-launch-screen-before-ini

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!