crazycodeboy / react-native-splash-screen

A splash screen for react-native, hide when application loaded ,it works on iOS and Android.
MIT License
5.6k stars 1.09k forks source link

RN 0.71 Splashscreen.show() not working on iOS #610

Open Engraulis61 opened 1 year ago

Engraulis61 commented 1 year ago

What react-native-splash-screen version are you using? 3.3.0 What platform does your issue occur on? (Android/iOS/Both) iOS i just want to show user splash screen after rnrestart.restart() command. but Splashscreen.show() method not working on iOS.

In older versions of React, I could solve this problem by adding code to the AppDelegate.mm file. However, since the AppDelegate.mm file has changed, I am unable to display the splash screen after using the RNrestart.Restart() command.

I used to use these codes in old versions of React Native:

RCTRootView *rootView = RCTAppSetupDefaultRootView(bridge, @"appname", nil);

UIStoryboard sb = [UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil]; UIViewController vc = [sb instantiateInitialViewController]; UIView* launchScreenView = vc.view; launchScreenView.frame = self.window.bounds; RCTRootView.loadingView = launchScreenView;

Is there anyone who can help with a solution for this problem?

robwalkerco commented 1 year ago

Duplicate of https://github.com/crazycodeboy/react-native-splash-screen/issues/606

Engraulis61 commented 1 year ago

I don't think it's the same topic.I tried the code block there, but splashscreen.show() didn't work after RNrestart.Restart().

a-tokyo commented 1 year ago

Change the code in your AppDelegate.mm to be

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
  self.moduleName = @"ProjectName";
  // You can add your custom initial props in the dictionary below.
  // They will be passed down to the ViewController used by React Native.
  self.initialProps = @{};

  bool didFinish = [super application:application didFinishLaunchingWithOptions:launchOptions];

  [RNSplashScreen show];  // this needs to be called after [super application:application didFinishLaunchingWithOptions:launchOptions];

  return didFinish;
}
Engraulis61 commented 1 year ago

Thank you for your help, but this solution did not work.

a-tokyo commented 1 year ago

If it's blocking your development, I highly recommend switching to this library https://www.npmjs.com/package/react-native-bootsplash - It's way more maintained and I am planing to move to it as well @Engraulis61

clarissacarcha commented 1 year ago

+1 same issue

Coffeegerm commented 11 months ago

@a-tokyo Your solution seemed to help in my case thanks! The docs for the package probably should just be updated to reflect this, but the best bet considering this package is largely unmaintained at this point is to move to bootsplash like you suggest.

arnoldc commented 5 months ago

Change the code in your AppDelegate.mm to be

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
  self.moduleName = @"ProjectName";
  // You can add your custom initial props in the dictionary below.
  // They will be passed down to the ViewController used by React Native.
  self.initialProps = @{};

  bool didFinish = [super application:application didFinishLaunchingWithOptions:launchOptions];

  [RNSplashScreen show];  // this needs to be called after [super application:application didFinishLaunchingWithOptions:launchOptions];

  return didFinish;
}

this solution still causes white flashes at 0.71 there is a proper way to do this, see below

Follow guide from the maintainers https://github.com/facebook/react-native/issues/35937#issuecomment-1617843557

At AppDelegate.mm, override createRootViewWithBridge

- (UIView *)createRootViewWithBridge:(RCTBridge *)bridge
                          moduleName:(NSString *)moduleName
                           initProps:(NSDictionary *)initProps
{
  UIView * view = [super createRootViewWithBridge:bridge moduleName:moduleName initProps:initProps];

#if RCT_NEW_ARCH_ENABLED
  RCTFabricSurfaceHostingProxyRootView * rootView = (RCTFabricSurfaceHostingProxyRootView *)view;
#else
  RCTRootView * rootView = (RCTRootView *)view;
#endif

  // workaround:
  UIStoryboard *sb = [UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil];
  UIViewController *vc = [sb instantiateInitialViewController];
  rootView.loadingView = vc.view;

  return rootView;
}

ive tested in ios 16 and ios 17 seems working fine now

LasithaOffice commented 4 months ago

Change the code in your AppDelegate.mm to be

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
  self.moduleName = @"ProjectName";
  // You can add your custom initial props in the dictionary below.
  // They will be passed down to the ViewController used by React Native.
  self.initialProps = @{};

  bool didFinish = [super application:application didFinishLaunchingWithOptions:launchOptions];

  [RNSplashScreen show];  // this needs to be called after [super application:application didFinishLaunchingWithOptions:launchOptions];

  return didFinish;
}

Worked beautifully