2016-07-04 16 views
8

Vì vậy, tôi đang làm việc trên một ứng dụng mới và đang sử dụng Firebase để quản lý tất cả nội dung phụ trợ với thông tin đăng nhập của người dùng, v.v. họ tải ứng dụng. Những gì tôi đang cố gắng làm là tải HomeScreenVC nếu người dùng đã đăng nhập và nếu người dùng không được thì họ sẽ được chuyển đến đăng nhập/đăng ký vc.Firebase + Swift: Bỏ qua Màn hình Đăng nhập Người dùng

Tôi loại nó hoạt động theo nghĩa là nó hoạt động nếu người dùng đã là người dùng hiện tại nhưng vấn đề tôi gặp phải là nếu người dùng chưa bao giờ tải ứng dụng sau đó nó bị lỗi vì không thể phát hiện currentUser .

Mã của tôi tôi có trong appdelegate là:

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 
} 

Đây là lỗi tôi nhận được: here

Bất kỳ trợ giúp sẽ là tuyệt vời! Tôi đang sử dụng phiên bản mới nhất của Firebase và Swift 2.

Trả lời

7

Bạn đang buộc unwrapping nil trên dòng đó, gây ra sự cố. Tháo ! từ FIRAuth.auth()?.currentUser!

+0

Làm việc !! Cảm ơn bạn rất nhiều!!!! – Konsy

0

cập nhật cho nhanh 3 và căn cứ hỏa lực 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 
} 
Các vấn đề liên quan