Swift 3实现iOS应用首次启动跳转指定页面、后续跳转首页的方法
Hey there! This is a really common pattern for iOS apps, and it’s totally doable with a few lines of code in Swift 3. Let’s break it down step by step.
The Core Idea
We need a way to persistently track whether the user has launched the app before. The simplest tool for this is UserDefaults—it’s lightweight, built into iOS, and perfect for storing small bits of persistent data like this flag.
Step 1: Check the Launch Flag in AppDelegate
In Swift 3, the main entry point for your app is still AppDelegate.swift, specifically the application(_:didFinishLaunchingWithOptions:) method. This is where we’ll decide which screen to show first.
For Storyboard-Based Apps
If you’re using Storyboards, make sure you’ve set unique Storyboard IDs for both your onboarding/initial screen and your home screen in Interface Builder. Then add this code to your AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Check if the app has been launched before let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "hasLaunchedBefore") let mainStoryboard = UIStoryboard(name: "Main", bundle: nil) var initialVC: UIViewController if !hasLaunchedBefore { // First launch: show your designated onboarding/setup screen initialVC = mainStoryboard.instantiateViewController(withIdentifier: "OnboardingVC") // Mark the app as launched for future sessions UserDefaults.standard.set(true, forKey: "hasLaunchedBefore") UserDefaults.standard.synchronize() // Important in Swift 3 to ensure the value is saved immediately } else { // Subsequent launches: show the home screen initialVC = mainStoryboard.instantiateViewController(withIdentifier: "HomeVC") } // Set the initial view controller as the root of the window self.window?.rootViewController = initialVC self.window?.makeKeyAndVisible() return true }
For Code-Based UI (No Storyboards)
If you’re building your UI entirely in code, skip the Storyboard instantiation and directly initialize your view controller classes:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "hasLaunchedBefore") var initialVC: UIViewController if !hasLaunchedBefore { initialVC = OnboardingViewController() UserDefaults.standard.set(true, forKey: "hasLaunchedBefore") UserDefaults.standard.synchronize() } else { initialVC = HomeViewController() } self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.rootViewController = initialVC self.window?.makeKeyAndVisible() return true }
Optional: Immediate Redirect After Onboarding Completion
If you want to send users to the home screen right after they finish your onboarding flow (instead of waiting for the next app launch), add this code to your onboarding screen’s "Finish" button action:
@IBAction func finishOnboardingTapped(_ sender: UIButton) { let homeVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC") UIApplication.shared.keyWindow?.rootViewController = homeVC }
Key Notes
- Storyboard IDs: Double-check that you’ve assigned the correct IDs in Interface Builder—if you get a crash here, it’s almost always because the ID doesn’t match what’s in your code.
- Persistence: The
hasLaunchedBeforeflag stays inUserDefaultsuntil the user uninstalls the app or clears its data. If you need to reset this (for testing), you can delete the app from your simulator/device and reinstall it. - Synchronize: In Swift 3, calling
synchronize()ensures theUserDefaultsvalue is written to disk immediately. Later versions of Swift don’t require this, but it’s safe to include here.
内容的提问来源于stack exchange,提问作者gopal

