Swift:使用Firebase自动登录用户

时间:2016-06-06 05:06:12

标签: ios swift firebase

我想自动登录用户,如果他已经签名并且只是前往主视图但代码运行了两次,你可以看到转换而不是刚刚显示的视图。我该如何解决?

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.slideMenuController
    FIRApp.configure()
    FIRAuth.auth()?.addAuthStateDidChangeListener {
        auth, user in
        if user != nil {
            // User is signed in.
            print("Automatic Sign In: \(user?.email)")

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController = storyboard.instantiateViewControllerWithIdentifier("EmployeeRevealViewController")
            self.window!.rootViewController = initialViewController

        } else {
            // No user is signed in.
        }
    }

    return true
}

日志

2016-06-06 01:00:55.585 Untitled[13009:6258910] Configuring the default app.
2016-06-06 01:00:55.657 Untitled[13009:] <FIRAnalytics/INFO> Firebase Analytics v.3200000 started
2016-06-06 01:00:55.666 Untitled[13009:] <FIRAnalytics/INFO> To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled
2016-06-06 01:00:55.714 Untitled[13009:6258910] Firebase Crash Reporting: Successfully enabled
2016-06-06 01:00:55.739: <FIRInstanceID/WARNING> FIRInstanceID AppDelegate proxy enabled, will swizzle app delegate remote notification handlers. To disable add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO
2016-06-06 01:00:55.739: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"
2016-06-06 01:00:55.760: <FIRMessaging/INFO> FIRMessaging library version 1.1.0
2016-06-06 01:00:55.781: <FIRMessaging/WARNING> FIRMessaging AppDelegate proxy enabled, will swizzle app delegate remote notification receiver handlers. Add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO
2016-06-06 01:00:55.788 Untitled[13009:] <FIRAnalytics/INFO> Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist
Automatic Sign In: Optional("mohamed.mohd@hotmail.com")
2016-06-06 01:00:56.759: <FIRInstanceID/WARNING> APNS Environment in profile: development
Automatic Sign In: Optional("mohamed.mohd@hotmail.com")
2016-06-06 01:00:57.811 Untitled[13009:] <FIRAnalytics/INFO> Firebase Analytics enabled

4 个答案:

答案 0 :(得分:7)

尝试:

if let alreadySignedIn = FIRAuth.auth()?.currentUser {
    // segue to main view controller
} else {
    // sign in
}

答案 1 :(得分:4)

至于更新后的文档,这是基于Firebase文档的推荐方法,它对我有用:

if Auth.auth().currentUser != nil {
  // User is signed in.
  // ...
} else {
  // No user is signed in.
  // ...
}

Firebase改变了他们的命名惯例,最引人注目的是:

FIRAuth已重命名为Auth

同样为了获得最佳效果,我把它放在viewDidAppear()中,如下所示:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    if Auth.auth().currentUser != nil {
        performSegue(withIdentifier: "yourIdentifier", sender: self)
    }
}

答案 2 :(得分:2)

没有Firebase 4和swift 4的标识符,它对我来说很好......

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    if Auth.auth().currentUser != nil {
        let newViewController: YourViewController = YourViewController()
        self.present(newViewController, animated: true, completion: nil)
    }
}

答案 3 :(得分:0)

如果用户!= nil,则使用功能“覆盖功能funD viewDidAppear”立即登录。您还忘记添加“视图”,使用self。视图。窗口

override func viewDidAppear(_ animated: Bool) {
        if Auth.auth().currentUser != nil {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController = storyboard.instantiateViewController(withIdentifier: "homeTabBarVC")
            self.view.window!.rootViewController = initialViewController
        } else { return }
    }