Firebase + Swift:绕过用户登录屏幕

时间:2016-07-04 19:30:15

标签: ios swift login firebase firebase-authentication

所以我正在开发一个新的应用程序,我正在使用Firebase来管理用户登录等所有后端内容,我在用户加载应用程序时为用户创建了一个登录/注册屏幕。我正在尝试做的是加载HomeScreenVC,如果用户已经登录,如果用户不是,那么他们将被定向到登录/注册vc。

如果用户已经是currentUser,那么它的工作方式是有效的,但我遇到的问题是,如果用户从未加载应用程序,那么它会崩溃,因为它无法检测currentUser。

我在AppDelegate中的代码是:

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
}

这是我得到的错误:here

任何帮助都会很棒!我正在使用最新版本的Firebase和Swift 2。

2 个答案:

答案 0 :(得分:7)

你是强行在那条线上展开nil,导致崩溃。从!

中删除FIRAuth.auth()?.currentUser!

答案 1 :(得分:0)

更新swift 3和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
}