Beginner Alert: I am doing Swift 11.6 with IOS 13.6. I am a beginner in Swift and I'm trying to check if my user has previously logged in or not. For this, I have used User Defaults, which seems to be working fine. The problem is when I initialize my view controller in Scenedelgate, it seems to not be working - I only get a black screen.
Another note is that I have a side menu on my home screen, so I'm trying to initialize the UINavigationController and then the HomeViewController inside of that
Here is my code for the scene delegate:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
var storyboard: UIStoryboard?
var view: UIView?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else {return}
let def = UserDefaults.standard
let is_authenticated = def.bool(forKey: "is_authenticated") // return false if not found or stored value
if is_authenticated {
// user logged in
self.view?.window?.rootViewController = UINavigationController(rootViewController: HomeViewController())
self.view?.window?.makeKeyAndVisible()
}
}
Edit 1: Here's how my user defaults function looks like:
func saveLoggedState() {
let def = UserDefaults.standard
def.set(true, forKey: "is_authenticated") // save true flag to UserDefaults
def.synchronize()
}
Edit 2: Here's how my simulator looks
Edit 3: Here's how my view controller looks
Edit 4: So here's the problem. I decided to try programmatically setting the home view controller to white, which obviously worked. So then what/s confusing me is that I can't see anything I put in my HomeViewController nor in my SideMenuNavigationController. I put a button and a label, but they're not showing up.
Thank you for any advice that you can give. I'm a complete newbie so I don't really know what I'm doing when it comes to this. Any help appreciated.
Thanks!
This line is wrong:
self.view?.window?.rootViewController = UINavigationController(rootViewController: HomeViewController())
The problem, in particular, is this phrase in your code:
HomeViewController()
That creates a new blank HomeViewController, not the instance you designed in the storyboard. To get that instance, tell the storyboard to instantiate it for you.