React-Native iOS - 如何在启动屏幕之后、初始化 React Native Bridge 之前显示视图控制器 (iOS)

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

我在原生 iOS 视图控制器 (swift) 中有一个动画(闪屏动画),我想在启动屏幕后显示该动画的视图 vontroller 3 秒在启动 react-native 桥之前。

我尝试使用 appdelegate.m 文件,这就是我的想法。

RN 版本 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

谢谢。

  • 您调用方法 [storyboard instantiateInitialViewController] 此方法将调用您为此故事板设置默认值的创建 ViewController。您是否已检查 Is Initial View Controller 以找到您要显示的第一个 ViewController?

  • 您可以使用另一种方式来初始化视图控制器[storyboard instantiateViewControllerWithIdentifier:@"myAnimationViewController"]; 记得为要设置 rootViewController.

  • 的视图控制器设置 Storyboard ID myAnimationViewController

代码示例:

- (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;
}