skip to Main Content

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)
     
            }
}

3

Answers


  1. Launch screen does not support any code insertion, it is just "image"screen rendered from Interface builder which is loaded before the app loads itself. I recommend using some other UIViewController that will look identical to your LaunchScreen and displays right after launchScreen, on this ‘standard’ UIViewControler you can perform any code you want as usual.

    Login or Signup to reply.
  2. 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.
    Login or Signup to reply.
  3. First of all Alert can be presented on View Controller and
    I think you should use UIAlertController which is an object which displays an alert message to user
    Following is the code you can try it will show the alert on top of ViewController

    let alert = UIAlertController(title: "Your title", message: "Your message", preferredStyle: .alert)
            let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
            alert.addAction(ok)
            DispatchQueue.main.async {
                self.present(alert, animated: true)
            }
    
    • Here self is referred to the my mainClass:ViewController on which i have to show alert and Dispatch Queue is used to show my UI if i am doing heavy operations in background or in another thread as UI can only be updated on main thread.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search