2016-07-04 151 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 
}