iOS应用launch screen仅首次显示的实现方案咨询
Hey there! Let's clear up one thing first: Apple's default Launch Screen (configured in LaunchScreen.storyboard/xib) is designed to display briefly every time your app launches, to give users immediate feedback that the app is loading. You can't disable this entirely, but if you want a custom intro/onboarding screen that only shows up the first time someone opens your app, here's exactly how to pull it off:
Core Idea
We'll use UserDefaults to store a simple flag tracking whether the app has been launched before. When the app starts, we check this flag:
- If it's not set (first launch), show your custom intro screen, then mark the flag as "launched".
- If it is set, skip the intro and go straight to your main app content.
Step-by-Step Implementation
1. Set Up the Flag Check
Let's cover both UIKit and SwiftUI since they're the two main frameworks used today.
For UIKit (iOS 13+)
If you're using SceneDelegate, add this logic in the scene(_:willConnectTo:options:) method:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } let window = UIWindow(windowScene: windowScene) self.window = window // Check if app has been launched before let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "hasLaunchedBefore") if hasLaunchedBefore { // Navigate to main screen let mainVC = MainViewController() // Replace with your main view controller window.rootViewController = UINavigationController(rootViewController: mainVC) } else { // Show custom launch/onboarding screen let launchVC = CustomLaunchViewController() // Replace with your custom launch screen VC window.rootViewController = UINavigationController(rootViewController: launchVC) // Mark app as launched UserDefaults.standard.set(true, forKey: "hasLaunchedBefore") UserDefaults.standard.synchronize() // Optional, ensures immediate save } window.makeKeyAndVisible() }
For SwiftUI
Use @AppStorage (a handy wrapper around UserDefaults) to simplify the check in your app's entry point:
@main struct YourApp: App { @AppStorage("hasLaunchedBefore") private var hasLaunchedBefore = false var body: some Scene { WindowGroup { if hasLaunchedBefore { MainContentView() // Your main app content } else { CustomLaunchView() // Your custom launch/onboarding view .onAppear { // Mark as launched after the view loads hasLaunchedBefore = true } } } } }
2. Handle Navigation from Custom Launch Screen
Add a way to navigate to the main app once the user is done with the intro (like a "Get Started" button). For example, in UIKit:
// Inside CustomLaunchViewController @IBAction func getStartedTapped(_ sender: UIButton) { let mainVC = MainViewController() // Push to main screen (or replace root to remove back navigation) navigationController?.pushViewController(mainVC, animated: true) }
3. Edge Cases to Consider
- Uninstall/Reinstall: If the user uninstalls and reinstalls the app,
UserDefaultswill be cleared, so the custom launch screen will show again (this is usually desired behavior). - Testing the First Launch Flow: To retest the intro, either uninstall the app, or add a debug-only button to reset the flag:
// Debug use only! UserDefaults.standard.set(false, forKey: "hasLaunchedBefore")
内容的提问来源于stack exchange,提问作者prem cool

