Yupi
Yupi

Reputation: 4470

SWRevealViewController delegate nil

at first place I'm just staring with iOS so maybe this is some dummy question but I can't figure it out. I'm using SWRevealViewController for side bar menu and I used delegate to detect FrontViewPosition and have this line of code: self.revealViewController().delegate = self in viewDidLoad and there problem appears (same thing if I put this line in viewDidAppear). I'm using FirebaseAuth and have LoginViewController so when I login first time exception is thrown

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

on line as I said self.revealViewController().delegate = self. Of course I can avoid this by simply nil checkin but then revealViewController() will not work as it is not registered.

Strange thing, at least for me, is next time after FirebaseAuth save my credentials and app starts directly on MainViewController everything will work. Only problem is when I login first time or log out and login again. I performSegue to launch MainViewController after login was successful. Can someone tell me where is the problem? Thanks

Upvotes: 1

Views: 275

Answers (2)

Yupi
Yupi

Reputation: 4470

Currently I found solution which works in my scenario. Still if someone knows what is the problem please explain. It would be my pleasure to mark your answer as correct one. I don't know is that a bug in SWRevealViewController or my approach wasn't correct. So what I did is:

I made checking is user logged in or not in MainViewController in viewWillAppear like this:

override func viewWillAppear(_ animated: Bool) {

    Auth.auth().addStateDidChangeListener() {
        auth, user in
        if user == nil {
            let loginController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
            self.present(loginController!, animated: true, completion: nil)
        }
    }
}

After that when user successfuly log in I just dismiss the LoginViewController and handle the user authentication where is necessary in MainViewController. And I tested this approach seems it is working. Same thing I did with SignUpViewController Thanks to everyone who tried to help.

Upvotes: 0

Sid Mhatre
Sid Mhatre

Reputation: 3417

I used SWRevealViewController in my project with login screen.

How I did to handle user login is:

  1. Make Login viewController as Initial View Controller.
  2. Then check for user already logged in or not inside didFinishLaunchingWithOptions in AppDelegate file.

    if (userDefaults.object(forKey:"user") != nil) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        self.window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
        self.window?.makeKeyAndVisible()
    }
    
  3. If user already logged in then present MainViewController.

  4. Then added below code in viewDidLoad of MainViewController.

    // To show Menu
    if (self.revealViewController() != nil) {
        menuButton.target = self.revealViewController()
        menuButton.action =  #selector(SWRevealViewController.revealToggle(_:))
        self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
    }
    

I hope this will helps you.

Upvotes: 1

Related Questions