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())
}
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