iosswiftfirebaseswrevealviewcontroller

Error setting root view controller with SWReveal in swift


I[m trying to set the root view controller in the app delegate based in a condition. When the app starts and set the root view controller a controller that has code to reveal the rear ViewController this error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value"

App Delegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    let db = Firestore.firestore()
    var docRef: DocumentReference!
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let authListener = Auth.auth().addStateDidChangeListener { (auth, user) in
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        if user != nil{
            var userId = user?.email
            docRef = Firestore.firestore().document("user/\(userId!)")
            docRef.getDocument(completion: { (docSnapshot, error) in
                guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
                let data = docSnapshot.data()
                variables.userType = data!["tipo"] as? String ?? ""
            })

            if variables.userType == "doctor" {
                let consulta = storyboard.instantiateViewController(withIdentifier: "ConsultaController")
                self.window?.rootViewController = consulta
                self.window?.makeKeyAndVisible()
            } else if variables.userType == "paciente"{

            }
        } else {
            let consulta = storyboard.instantiateViewController(withIdentifier: "ConsultaController")
            self.window?.rootViewController = consulta
            self.window?.makeKeyAndVisible()
        }
    }
    return true
}

code in the view controller where the error is triggered.

EDIT: error is triggered when the initial view controller is loaded. The initial view controller is determined by condition in the app delegate

override func viewDidLoad() {
    super.viewDidLoad()
    setUpSWReveal()
}

func setUpSWReveal(){
    menuBtn.target = self.revealViewController()
    menuBtn.action = #selector(SWRevealViewController.revealToggle(_:))
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) //error triggered here
    self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
}

Solution

  • I fixed it by setting the front and rear view controller in each condition. im using a navigation controller so some parts of the code below are properties of a navigarion controller

     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
        FirebaseApp.configure()
        let db = Firestore.firestore()
        var docRef: DocumentReference!
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
        let authListener = Auth.auth().addStateDidChangeListener { (auth, user) in
            if user != nil{
                var userId = user?.email
                docRef = Firestore.firestore().document("user/\(userId!)")
                docRef.getDocument(completion: { (docSnapshot, error) in
                    guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
    
                    let data = docSnapshot.data()
                    variables.userType = data!["tipo"] as? String ?? ""
                    if variables.userType == "paciente" {
                        let frontNavigationController:UINavigationController
                        let revealController = SWRevealViewController()
                        var mainRevealController = SWRevealViewController()
    
                        let front = storyboard.instantiateViewController(withIdentifier:  "MedicionViewContoller") as! MedicionVC
                        let rear = storyboard.instantiateViewController(withIdentifier: "MenuController") as! MenuVC
    
                        frontNavigationController =  UINavigationController.init(rootViewController: front)
                        frontNavigationController.navigationBar.barTintColor = UIColor.init(red: 12.0/255.0, green: 73.0/255.0, blue: 120.0/255.0 , alpha: 1.0)
                        frontNavigationController.navigationBar.titleTextAttributes = [
                            NSAttributedString.Key.foregroundColor : UIColor.white,
                            NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 45)!
                        ]
                        frontNavigationController.navigationItem.leftBarButtonItem?.action = #selector(SWRevealViewController.revealToggle(_:))
                        revealController.frontViewController = frontNavigationController
                        revealController.rearViewController = rear
                        revealController.delegate = self
                        mainRevealController  = revealController
    
                        UIApplication.shared.delegate!.window??.rootViewController = mainRevealController
                        //
                    }
                    else {
                        let frontNavigationController:UINavigationController
                        let revealController = SWRevealViewController()
                        var mainRevealController = SWRevealViewController()
    
                        let front = storyboard.instantiateViewController(withIdentifier:  "ConsultaController") as! ConsultaVC
                        let rear = storyboard.instantiateViewController(withIdentifier: "MenuController") as! MenuVC
    
                        frontNavigationController =  UINavigationController.init(rootViewController: front)
                        frontNavigationController.navigationBar.barTintColor = UIColor.init(red: 12.0/255.0, green: 73.0/255.0, blue: 120.0/255.0 , alpha: 1.0)
                        frontNavigationController.navigationBar.titleTextAttributes = [
                            NSAttributedString.Key.foregroundColor : UIColor.white,
                            NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 45)!
                        ]
                        frontNavigationController.navigationItem.leftBarButtonItem?.action = #selector(SWRevealViewController.revealToggle(_:))
                        revealController.frontViewController = frontNavigationController
                        revealController.rearViewController = rear
                        revealController.delegate = self
                        mainRevealController  = revealController
    
                        UIApplication.shared.delegate!.window??.rootViewController = mainRevealController
                    }
                })
            }else{
                let login = storyboard.instantiateViewController(withIdentifier: "LogInController")
                self.window?.rootViewController = login
                self.window?.makeKeyAndVisible()
            }
        }
    
        return true
    }