Ios Swift:使用Firebase自动登录用户

Ios Swift:使用Firebase自动登录用户,ios,swift,firebase,Ios,Swift,Firebase,我想自动登录的用户,如果他已经签署,只是头到主视图,但代码正在运行两次,你可以看到传输,而不是视图只是显示。我怎么修理它 AppDelegate.swift func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization afte

我想自动登录的用户,如果他已经签署,只是头到主视图,但代码正在运行两次,你可以看到传输,而不是视图只是显示。我怎么修理它

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未命名[13009:6258910]配置默认应用程序。
2016-06-06 01:00:55.657未命名[13009:]Firebase Analytics v.3200000已启动
2016-06-06 01:00:55.666未命名[13009:]要启用调试日志记录,请设置以下应用程序参数:-FIRAnalyticsDebugEnabled
2016-06-06 01:00:55.714未命名[13009:6258910]Firebase崩溃报告:已成功启用
2016-06-06 01:00:55.739:FIRInstanceID AppDelegate代理已启用,将swizzle应用程序代理远程通知处理程序。要禁用,请将“FirebaseAppDelegateProxyEnabled”添加到您的Info.plist并将其设置为否
2016-06-06 01:00:55.739:无法获取APNS令牌错误域=com.firebase.iid代码=1001“(空)”
2016-06-06 01:00:55.760:FIRMessaging库版本1.1.0
2016-06-06 01:00:55.781:启用FIRMessaging AppDelegate代理,将swizzle app delegate远程通知接收器处理程序。将“FirebaseAppDelegateProxyEnabled”添加到Info.plist并将其设置为否
2016-06-06 01:00:55.788未命名[13009:]已成功自动创建Firebase Analytics应用程序代理。要禁用代理,请在Info.plist中将标志FirebaseAppDelegateProxyEnabled设置为NO
自动登录:可选(“mohamed。mohd@hotmail.com")
2016-06-06 01:00:56.759:APNS环境概况:发展
自动登录:可选(“mohamed。mohd@hotmail.com")
2016-06-06 01:00:57.811未命名[13009:]启用Firebase分析
试试:


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

if Auth.auth().currentUser != nil {
  // User is signed in.
  // ...
} else {
  // No user is signed in.
  // ...
}
Firebase更改了一个名称,但他们的命名约定,最值得注意的是:

FIRAuth被重命名为Auth

此外,为了获得最佳效果,我将其放在ViewDidAspect()中,如下所示:

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

没有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)
    }
}
如果用户!=无您还忘了添加“视图”,请使用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 }
    }

您是否记录了身份验证数据?遵循firebase的此文档:我遵循了该文档。它可以工作,但代码只运行了日志末尾显示的两倍。然后是从主屏幕到主屏幕的过渡。你确定你在其他VC中没有做任何与身份验证相关的事情吗?从主屏幕到主屏幕的转换是什么?记住不要将代码放在
viewDidLoad()
中,而是放在
viewDidAppear
中。我犯了那个错误,并且不知道为什么即使调用了seque也没有通过。当用户关闭并重新打开应用程序时会发生什么?viewDidAppear的问题是登录页面(或调用此功能的任何页面)会出现一微秒,然后加载“yourIdentifier”。。。有什么解决方案吗?@DragonFire您应该在假定用户已登录的页面上启动应用程序,然后
如果Auth.Auth().currentUser==nil
,则在顶部显示登录屏幕。
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    if Auth.auth().currentUser != nil {
        let newViewController: YourViewController = YourViewController()
        self.present(newViewController, animated: true, completion: nil)
    }
}
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 }
    }