【问题标题】:iOS Swift Firebase Checking user at app startup not workingiOS Swift Firebase在应用程序启动时检查用户不起作用
【发布时间】:2020-06-13 00:56:55
【问题描述】:

我用 firebase 开发了一个应用程序。应用程序会检查是否有用户在每次启动时登录到 sceneDelegate.swift 文件中。但它仅适用于 iOS 13 设备。 iOS 12 设备用户每次都必须登录。我该如何解决这个问题?谢谢。

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
@available(iOS 13.0, *)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

    let currentUser = Auth.auth().currentUser

    if currentUser != nil {

        let board = UIStoryboard(name: "Main", bundle: nil)
        let navigationController = board.instantiateViewController(identifier: "navigationController") as! UINavigationController
        window?.rootViewController = navigationController

    }


    guard let _ = (scene as? UIWindowScene) else { return }
}

【问题讨论】:

    标签: ios swift firebase firebase-authentication ios12


    【解决方案1】:

    SceneDelegate 是在 iOS 13 中引入的。对于 iOS 12 及更低版本,您应该在 AppDelegatedidFinishLaunchingWithOptions 方法中实现相同的逻辑,如下所示:

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            window = UIWindow()
            window?.makeKeyAndVisible()
            let currentUser = Auth.auth().currentUser
    
            if currentUser != nil {
                let board = UIStoryboard(name: "Main", bundle: nil)
                let navigationController = board.instantiateViewController(withIdentifier: "navigationController") as! UINavigationController
                window?.rootViewController = navigationController
            }
            return true
        }
    }
    

    【讨论】:

    • 我在 appDelegate 的 didFinishLaunchingWithOptions 函数中添加了相同的代码,但是 Xcode 显示警报“instantiateViewController(identifier:creator:)' is only available in iOS 13.0 or later”我该如何解决这个问题?
    • board 上使用instantiateViewController(withIdentifier: 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多