0

AppDelegate.m
这工作正常(当应用程序进入后台时显示启动)

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NativeScreens" bundle:nil];
  SplashVC *SplashViewController=[storyboard instantiateViewControllerWithIdentifier:@"SplashVC"];
  [self.window.rootViewController presentViewController:SplashViewController animated:NO completion:NULL];
}

但现在我需要从 react-native 调用它。
我创建了一个具有相同功能的方法:

RCT_EXPORT_METHOD(showCustomNativeSplashScreen)
{
  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NativeScreens" bundle:nil];
  SplashVC *SplashViewController=[storyboard instantiateViewControllerWithIdentifier:@"SplashVC"];
  [self.window.rootViewController presentViewController:SplashViewController animated:NO completion:NULL];
}

它在 RN 中可用,(我从 console.log 中看到它)但是当我调用它时 - 没有任何反应。

import { NativeModules } from 'react-native';
...
console.log(NativeModules.AppDelegate); // Object{showCustomNativeSplashScreen: function}
NativeModules.AppDelegate.showCustomNativeSplashScreen(); // nothing :(

我究竟做错了什么?

4

1 回答 1

0

需要找到上位控制器

- (UIViewController *)topViewController{
  return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
  if (rootViewController.presentedViewController == nil) {
    return rootViewController;
  }

  if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
    UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
    UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
    return [self topViewController:lastViewController];
  }

  UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
  return [self topViewController:presentedViewController];
}

 RCT_EXPORT_METHOD(showCustomNativeSplashScreen)
 {
   self.lastTopVC = [self topViewController];
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NativeScreens" bundle:nil];
   SplashVC *SplashViewController=[storyboard instantiateViewControllerWithIdentifier:@"SplashVC"];
   [self.lastTopVC presentViewController:SplashViewController animated:NO completion:NULL];
 }
于 2018-07-20T21:49:01.647 回答