Different view controller display at start app

768 Views Asked by At

I try to explain situation: I have two view controllers: viewHome and viewStartTest. When student start app first time, don't have any data about his test in table. In this situation should be display viewStartTest controller after launch screen. But when he start app again and condition "test is finished" is true, viewHome controller should be display at start. I try to put this code in AppDelegate.swift and simulate finished test but still not working, thanks for help:

// 0 - false, 1 - True
var conditionTest = 1

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    if conditionTest == 1 {

        self.window = UIWindow(frame: UIScreen.main.bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewStartTest: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewStartTest")
        self.window?.rootViewController = viewStartTest
        self.window?.makeKeyAndVisible()

    } else {

        self.window = UIWindow(frame: UIScreen.main.bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let ViewHome: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewHome")
        self.window?.rootViewController = ViewHome
        self.window?.makeKeyAndVisible()
    }
}


Correct Scene delegate code after discussion below:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
          if conditionTest == 1 {


        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewStartTest: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewStartTest")
        self.window?.rootViewController = viewStartTest
        self.window?.makeKeyAndVisible()

    } else {


        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let ViewHome: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewHome")
        self.window?.rootViewController = ViewHome
        self.window?.makeKeyAndVisible()
    }
}
        guard let _ = (scene as? UIWindowScene) else { return }
    }

1

There are 1 best solutions below

0
On

You need to store the value of conditionTest somewhere. I would suggest using UserDefaults . This is an example of how you could implement it:

NavigationController:

class MainNavigationControllerViewController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()

    if isLoggedIn() {
        let homeController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC")
        viewControllers = [homeController]
    }
}

fileprivate func isLoggedIn() -> Bool {
    return UserDefaults.standard.isLoggedIn()
}
}

Extension for UserDefaults:

extension UserDefaults {

func setIsLoggedIn(value: Bool) {
    set(value, forKey: "isLoggedIn")
    synchronize()
}

func isLoggedIn() -> Bool {
    return bool(forKey: "isLoggedIn")    }
}

How to use:

with the above code you can simply login/logout the user. Make sure to call .synchronize()

Login:

UserDefaults.standard.setIsLoggedIn(value: true)
UserDefaults.standard.synchronize()

Logout:

UserDefaults.standard.setIsLoggedIn(value: false)
UserDefaults.standard.synchronize()