2016-07-04 120 views
8

因此,我正在开发一款新应用,并且我正在使用Firebase来管理用户登录等所有后端内容,我为用户创建了登录/注册屏幕他们加载应用程序。我想要做的是加载HomeScreenVC,如果用户已经登录,如果用户不是,那么他们会被引导到登录/注册vc。Firebase + Swift:绕过用户登录屏幕

如果用户已经是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的最新版本。

回答

7

您正在强制展开零线,导致崩溃。卸下!FIRAuth.auth()?.currentUser!

+0

工作!非常感谢!!!! – Konsy

0

更新迅速3和火力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 
}