swift - 如何在 Firebase iOS13+ 中保持登录状态
问题描述
这个问题已经在这里问过了,解决方案不再相关,因为iOS13有一些变化。关闭应用程序并重新打开后如何保持登录状态。
我有WelcomeViewController
,未登录时出现的,以及CoffeeViewController: UIViewController
用户登录时应该出现的类。
class CoffeeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
checkAuthn()
}
func checkAuthn() {
if Auth.auth().currentUser == nil {
DispatchQueue.main.async {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "welcomeScreen") as? WelcomeViewController
self.present(initialViewController!, animated: true, completion: nil)
}
} else {
print("Done")
}
}
}
SceneDelegate
我知道我应该在(以前是在didFinishLaunchingWithOptions
方法中)实现 rootview 控制器。而且我不知道如何正确地制作它。我尝试过这种方式,但它对我不起作用。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
let vc = CoffeeViewController()
let nc = UINavigationController(rootViewController: vc)
window?.rootViewController = nc
window?.makeKeyAndVisible()
}
- - - - - - - - - - - - -第2部分 - - - - - - - - - - - - ------------------
在sceneDelegate发生变化之后:
guard let windowsScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = UINavigationController(rootViewController: CoffeeViewController())
window?.makeKeyAndVisible()
我收到错误:
-------------------------第 3 部分解决方案---------- ------------
我替换了 sceneDelegate 中的代码,它工作正常。
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let homeVC = UIStoryboard(name:"Main", bundle: nil).instantiateViewController(withIdentifier: "ShopsMain") as! CoffeeViewController
let navC = UINavigationController(rootViewController: homeVC)
window?.rootViewController = navC
window?.makeKeyAndVisible()
解决方案
您需要初始化window
in UISceneDelegate
。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = UINavigationController(rootViewController: CoffeeViewController())
window?.makeKeyAndVisible()
}
推荐阅读
- python-3.x - 无法让 while 循环按照我想要的方式工作
- python - 更改数据帧索引中的数字格式
- python - 熊猫读取 csv 只得到 1 列
- c - 多线程实现使用 2 个线程读取、旋转和保存图像
- neo4j - Neo4j 在展开后使用返回 null 的理解列表保持绑定变量
- reactjs - 是否应该对在其 props 中带有“children”的 HOC 组件进行 momoized
- web-scraping - ImportXML 返回导入的内容为空
- .net - 如何在不使用强类型的情况下获取富文本中的组件对象 [Kontent Delivery SDK .NET]
- node.js - 反应js nodejs socket.io
- sql-server - 使用 T-SQL,如何在一个过程中替换多个变量?提供的示例