jonbhanson / flutter_native_splash

Automatically generates native code for adding splash screens in Android and iOS. Customize with specific platform, background color and splash image.
https://pub.dev/packages/flutter_native_splash
MIT License
1.35k stars 214 forks source link

[iOS] Splash Screen disappears and shows black screen when using new ```FlutterEngine```. #445

Closed dayoul closed 2 years ago

dayoul commented 2 years ago

version - 2.2.8 platform - iOS

Hi. First of all, thank you for the wonderful package. Everything works fine except for the situation in iOS, when using FlutterEngine from FlutterEngineGroup . My initial ViewController is like below.

import Flutter
import Foundation

class MainFlutterViewController: FlutterViewController {
    required init(coder aDecoder: NSCoder) {
        NSLog("init MainFlutterViewController NSCoder:::")
        let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.mainEngine = appDelegate.engines.makeEngine(withEntrypoint: "main", libraryURI: nil)
        GeneratedPluginRegistrant.register(with: appDelegate.mainEngine!)
        super.init(engine: appDelegate.mainEngine!, nibName: nil, bundle: nil)
    }
}

At first, splash screen appears, but when my main() function in dart gets called(probably when my mainEngine starts running), splash screen disappears and shows black empty screen for a moment before runApp().

Below is my main.dart code.

Future<void> main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);

  runApp(App());
  FlutterNativeSplash.remove();
}

Any idea for this?. Thanks in advance.

jonbhanson commented 2 years ago

If you remove the FlutterNativeSplash.remove(), does the splash screen still get removed? I think your runApp call is going to trigger a bunch of async code, so it may be that it looks like the splash screen is removed before runApp but it is really just the delay of the async code that makes it appear that way. I expect you might have better results if you run the FlutterNativeSplash.remove() somewhere inside runApp after you have loaded the resources your app depends upon. Just a guess, though.

dayoul commented 2 years ago

I've found a workaround for this. As I'm creating new FlutterViewController(not using default one), I had to register the splash screen to FlutterViewController().splashScreenView. So the code looks like this.

class MainFlutterViewController: FlutterViewController {
    required init(coder aDecoder: NSCoder) {
        ...
        let storyboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
        let aVC = storyboard.instantiateViewController(withIdentifier: "LaunchScreenViewController") as? UIViewController
        splashScreenView = aVC!.view
    }
}

Closing the issue. Thanks for the response(@jonbhanson) by the way.

sherazipet commented 1 year ago

Hi! Still have this issue with default iOS flutter app. Package version 2.2.16. @jonbhanson I move remove before and after runApp but result is the same.

jonbhanson commented 1 year ago

@sherazipet please open a new ticket and supply the full details requested in the ticket template.