iosswiftxcodeswift5launch-screen

How to show an alert view on the top of the launch screen in iOS?


I am using LaunchScreen.storyboard for showing the splash screen which is just a static image. Now the problem is i want to show an alert on the top of this splash screen if a particular condition is not met . For this i did the following code . But unfortunately the alert is shown only after the splash screen. How can i show the alert on the top of splash screen? As of now this is the following code which I am implementing but the splash screen does not shows the alert instead it shows only after the splash screen.

    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
​
    if conditionMet
            {
                print("Condition met!")
            }
            else
            {
                
                let storyboard : UIStoryboard = UIStoryboard(name: "Launch Screen", bundle: nil)
                let launchScreenCtrler = storyboard.instantiateViewController(identifier: "LAUNCH_SCREEN")
                let alertViewFrame = CGRect(x: 100, y: 300, width: 120, height: 80)
                let newView : UIView = UIView(frame: alertViewFrame)
                launchScreenCtrler.view.addSubview(newView)
                launchScreenCtrler.view.bringSubviewToFront(newView)
  
                alert.show(launchScreenCtrler, sender: newView)
     
            }
}

Solution

  • You can't control what is visible over Launch Screen or for how much time it is displayed.

    Here's what you should do -

    1. Create a new screen called SplashViewController that has identical layout as LaunchScreen.storyboard.
    2. Make sure this SplashViewController is the first screen to be visible to user in the app.
    3. Inside this screen you now have full control over what you want to do - show an alert, any other animation etc.
    4. In case you find out that you need to show the alert, you enter the new flow with alert.
    5. In case you find out that you don't need to show the alert, you enter the old flow without the alert.