使入职仅出现一次

Make Onboarding only appear once

我知道 Whosebug 上有很多这样的问题,但我似乎无法正确回答。基本上我有代码可以正确打印天气与否这是应用程序是否第一次启动。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
    
    UserDefaults.standard.set(true, forKey: "launchedBefore")
    
    if launchedBefore  {
        print("Not first launch.")
    }
        
    else {
        print("First launch, setting UserDefault.")
        
        UserDefaults.standard.set(true, forKey: "launchedBefore")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "onboardingSB")
        self.window?.rootViewController = viewController
        self.window?.makeKeyAndVisible()
    }
    
    return true
}

我只是不明白为什么它不会在第一次尝试时在载入视图控制器上打开。

初始视图控制器是 'Main View Controller'

主要故事板 ID:Main

入职故事板 ID:onboardingSB

请帮忙:)

在 AppDelegate 中的 didFinishLaunchingWithOptions 中添加:

if(UserDefaults.standard.bool(forKey: "notFirstInApp") == false){
    UserDefaults.standard.set(true, forKey: "notFirstInApp")
    //Here you can show storyboard that you have to launch at first launch
}else{
    //Here you can show storyboard that you have to launch after first launch
}

这很好用。

就像你的情况一样,

if(UserDefaults.standard.bool(forKey: "notFirstInApp") == false){
    UserDefaults.standard.set(true, forKey: "notFirstInApp")
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyboard.instantiateViewController(withIdentifier: "onboardingSB")
    self.window?.rootViewController = viewController
    self.window?.makeKeyAndVisible()
}else{
   //Here you can show storyboard that you have to launch after first launch
}

出于某种原因,我无法在应用程序委托中执行此操作。不过我已经找到解决办法了。

我首先将初始视图控制器设置为引导屏幕

然后我把这个加到视图中会出现:

override func viewWillAppear(_ animated: Bool) {
        
        if Core.shared.isNewUser(){
            Core.shared.notNewUser()
            
            print("new user")
        }
            
        else{
            print("existing user")
            
            let mainAppViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainViewController")
            
            if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                let sceneDelegate = windowScene.delegate as? SceneDelegate,
                let window = sceneDelegate.window{
                
                window.rootViewController = mainAppViewController
                
                UIView.transition(with: window,
                                  duration: 0.25,
                                  options: .transitionCrossDissolve,
                                  animations: nil,
                                  completion: nil)
            }
        }
        
    }

核心class:

class Core{
    
    static let shared = Core()
    
    func isNewUser() -> Bool{
        return !UserDefaults.standard.bool(forKey: "isNewUser")
    }
    
    func notNewUser(){
        UserDefaults.standard.set(true, forKey: "isNewUser")
    }

感谢:https://www.youtube.com/watch?v=7GFgmjZ4r2c&t=434s