如何仅在首次启动 iOS 应用程序 (Swift) 时呈现不同的视图控制器 - 以编程方式

How to present a different view controller only at first launch of iOS app (Swift) - programmatically

第一次下载我的应用程序时,我想推送一个不同的视图控制器(例如教程视图控制器),然后在再次打开后,我想在启动时推送我的普通视图控制器。在 swift 中,我推送视图控制器的方式如下:

func application(_ application: UIApplication, 
didFinishLaunchingWithOptions launchOptions: 
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)
    window!.rootViewController = ViewController()
    window!.makeKeyAndVisible()

    return true
}

所以直觉上,可能是这样的:

func application(_ application: UIApplication, 
didFinishLaunchingWithOptions launchOptions: 
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)

    if firstRun == true {
        window!.rootViewController = LaunchViewController()
        window!.makeKeyAndVisible()
    } else {
        window!.rootViewController = ViewController()
        window!.makeKeyAndVisible()
    }

    return true
}

您可以尝试在 UserDefaults

中保存该状态
if !UserDefaults.standard.bool(forKey: "LaunchedBefore") {
    window!.rootViewController = LaunchViewController()
    UserDefaults.standard.set(true, forKey: "LaunchedBefore")
} else {
    window!.rootViewController = ViewController()

}
window!.makeKeyAndVisible()