0

I'm trying to set up an instruction-like page showing when the app is first downloaded or re-downloaded. I want instructionIntro to show on first launch, and Main to appear the rest of the time. This code is in my GuideBookIntroClass. Can anyone provide a better solution or what the issue in my code is? It's still showing my old intro page into Main.storyboard.

let isFirstLaunch = UserDefaults.standard.value(forKey: "isFirstLaunch") as? Bool
            
            if isFirstLaunch == true {
            //It's the initial launch of application.
                let storyBoard : UIStoryboard = UIStoryboard(name: "instructionIntro", bundle:nil)
                let mainViewController = storyBoard.instantiateViewController(withIdentifier: "instructionIntro") as! InstructionIntroViewController
                let navigationController = UINavigationController(rootViewController: mainViewController)
                if let window = UIApplication.shared.delegate?.window {
                      window?.rootViewController = navigationController
                }
            }
        
            else {
                
            // not initial launch
                
                let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                let mainViewController = storyBoard.instantiateViewController(withIdentifier: "Main") as! GuideBookIntroClass
                let navigationController = UINavigationController(rootViewController: mainViewController)
                if let window = UIApplication.shared.delegate?.window {
                      window?.rootViewController = navigationController
                }
            

            }
Parker
  • 1
  • 1
  • Does this answer your question? [How to detect first time app launch on an iPhone](https://stackoverflow.com/questions/9964371/how-to-detect-first-time-app-launch-on-an-iphone) – timbre timbre Aug 05 '22 at 17:01
  • If the user has _any_ value for your app in `UserDefaults`, it means they are not launching your app for the first time. So at the minimum you should default retrieved value to `true` (i.e. if there's no value in `UserDefaults`, then user is launching the app for the first time): `let isFirstLaunch = UserDefaults.standard.value(forKey: "isFirstLaunch") as? Bool ?? true` Also don't forget to save value once you navigated to intro view: ` if isFirstLaunch { UserDefaults.standard.set(false, forKey: "isFirstLaunch")` – timbre timbre Aug 05 '22 at 17:03

0 Answers0