0

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!

Gryffin
  • 99
  • 1
  • 8
  • A scene delegate has no view, and giving it one doesn't do anything, so what's the point of talking about its view? Again, a scene delegate has no storyboard. The code in general makes no sense. If the question is how to do a login screen in the scene delegate, I have a working example here: https://github.com/mattneub/RegistrationExample/tree/master/RegistrationExample – matt Aug 08 '20 at 15:06
  • As for the blackness of the view controller, you have not shown anything about your view controller, so how do we know it _isn't_ black? For all we know, your code is working _perfectly_! – matt Aug 08 '20 at 15:10
  • Hi, thanks for suggestions. I'll add how my home view controller looks in the edit, hopefully that helps. – Gryffin Aug 08 '20 at 17:20

2 Answers2

0

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • And see my article on this topic, http://www.programmingios.net/dont-make-a-new-instance-by-mistake-2/ – matt Aug 09 '20 at 02:00
  • Thanks! I didn't realize this at all. Appreciate it – Gryffin Aug 09 '20 at 16:47
  • Oh sorry, I tried to accept the solution for both of them,but that didn't work, I'll just change it to yours! Sorry about that. I'm new to stack overflow. – Gryffin Aug 09 '20 at 17:20
  • And if you're wondering, I tried to accept Fabio's because originally I was using self and view instead of a window. Thank you both again for your help! – Gryffin Aug 09 '20 at 17:21
  • I'm not saying you should accept mine, I'm saying that the other answer is wrong because it makes exactly the same mistake as yours, so that it cannot be right. You yourself said it got you the same black screen as before. – matt Aug 09 '20 at 17:39
-1

Try to add windowScene and present the root controller correctly like this:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

     guard let windowScene = (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
      window = UIWindow(windowScene: windowScene)
      window?.makeKeyAndVisible()
      let controller = UINavigationController(rootViewController: HomeViewController())
      window?.rootViewController = controller
    }
 }

if your user defaults work correct the HomeViewController() show you...

Fabio
  • 5,432
  • 4
  • 22
  • 24
  • Hi, I just tried doing this but I still have the same black screen. Can someone still help? – Gryffin Aug 08 '20 at 06:39
  • Have you copy and paste all in the code? Because it work perfect!!! what's in the HomeController? Is it a empty controller? – Fabio Aug 08 '20 at 07:00
  • Don't correct your code, copy and paste mine...Are you designing UI in storyboard or programmatically? – Fabio Aug 08 '20 at 07:09
  • Hi Fabio! Thanks for taking the tme to reply again. The HomeController has a bar button in the navigation control for a side menu and if it works you should see a white menu with a button in the corner. Do you want me to delete the original code and then add yours? Because that's what I did for mine. Also, I am designing the UI both in storyboard and programmatically, though it's mostly in storyboard. – Gryffin Aug 08 '20 at 14:45
  • See the thing is, it's not completely black. You can see a white bar at the top which means there's a navigation bar, but the problem is the actual home screen is black. Any ideas why? I'll try sending a screenshot. – Gryffin Aug 08 '20 at 15:00