Firebase + Swift: Bypass Login Screen

So, I'm working on a new application, and I use Firebase to manage all the backend files with user inputs, etc., I created a login / registration screen for users when loading the application. What I'm trying to do is load HomeScreenVC, if the user is already logged in, and if the user is not, then they will be sent to login / register vc.

I kind of work in the sense that it works if the user is already currentUser, but the problem I'm having is that if the user has never downloaded the application, it crashes because it cannot detect currentUser.

My code that I have in AppDelegate is:

var window: UIWindow? var storyboard: UIStoryboard? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. FIRApp.configure() self.storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let currentUser = FIRAuth.auth()?.currentUser! if currentUser != nil { self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("tabVC") } else { self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("loginScreen") } return true } 

This is the error I get: here

Any help would be great! I am using the latest version of Firebase and Swift 2.

+8
ios login swift firebase firebase-authentication
source share
2 answers

You force to reverse zero on this line, causing a crash. Remove it ! from FIRAuth.auth()?.currentUser!

+7
source share

update for swift 3 and firebase 4.8

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { UIApplication.shared.statusBarStyle = .lightContent // Override point for customization after application launch. // add firebase FirebaseApp.configure() // check user status then set initial Viewcontroller self.window = UIWindow(frame: UIScreen.main.bounds) let storyboard = UIStoryboard(name: "Main", bundle: nil) let startMainVC = storyboard.instantiateViewController(withIdentifier: "MainVC") let startIntroVC = storyboard.instantiateViewController(withIdentifier: "IntroVC") // check if user is logged if authProvider.Instance.userStatus() == true{ self.window?.rootViewController = startMainVC } else { self.window?.rootViewController = startIntroVC } return true } 
0
source share

All Articles