我想在我的SwiftUI应用首次启动时启动一个教程。该代码应在项目中的什么位置以及如何在首次启动应用程序时启动只是SwiftUI View的教程?
我已经知道如何在使用UserDefaults之前检查应用程序是否已启动。我想知道如何启动SwiftUI视图,然后在用户完成本教程后如何启动标准ContentView。
let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "hasLaunchedBefore")
if hasLaunchedBefore {
// Not first launch
// Load ContentView here
} else {
// Is first launch
// Load tutorial SwiftUI view here
UserDefaults.standard.set(true, forKey: "hasLaunchedBefore") // Set hasLaunchedBefore key to true
}
尝试将其放在您的场景中
let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "hasLaunchedBefore")
let content = ContentView()
let tutorial = TutorialView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
if hasLaunchedBefore {
window.rootViewController = UIHostingController(rootView: content)
} else {
window.rootViewController = UIHostingController(rootView: tutorial)
UserDefaults.standard.set(true, forKey: "hasLaunchedBefore")
}
self.window = window
window.makeKeyAndVisible()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句