簡體   English   中英

iOS 13.2 的登錄導航/Segue 使用 Firebase

[英]LogIn Navigation/Segue for iOS 13.2 using Firebase

我正在嘗試使用Firebase為我的iOS 13.2應用程序實現SignIn 用戶登錄后,如何實現從登錄頁面到 HomeScreen(a ViewController ) 的segue

有一個方法通過GIDSignInDelegate附加到AppDelegate ,它會在用戶登錄時通知我們。我想在segue轉到主屏幕。 此代碼在AppDelegate中,由於新的SceneDelegate行為,我無法使用AppDelegate's windowStoryBoard加載。

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if (error == nil) {
          // Perform any operations on signed in user here.
          // ...
            print("User signed in")

            //place to perform segue
            //write code for segue here

        }
        else {
          print("\(error.localizedDescription)")
        }
        if user != nil
        {
        guard let authentication = user.authentication else { return }
          let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                            accessToken: authentication.accessToken)
          // ...
        Auth.auth().signIn(with: credential) { (authResult, error) in
          if let error = error {
            // ...
            return
          }
          // User is signed in
          // ...          
        }
        }       
    }   

預期結果: https://stackoverflow.com/a/27136455/6311566但這在iOS 13.2中不起作用

老路

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        let rootVC = window?.rootViewController

        return true
    }

這是因為 AppDelegate.swift 不再具有 window 屬性。 現在您必須使用 SceneDelegate.swift 來更改根視圖 controller。 如本例所示:

現在要做什么

現在,您將把代碼移到應用的 SceneDelegate Swift 文件中:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

     var window: UIWindow?


    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).
        guard let _ = (scene as? UIWindowScene) else { return }

        // Create the root view controller as needed
        let vc = ViewController()
        let nc = UINavigationController(rootViewController: vc)

        // Create the window. Be sure to use this initializer and not the frame one.
        let win = UIWindow(windowScene: winScene) 
        win.rootViewController = nc
        win.makeKeyAndVisible()
        window = win
    }

    // ... the rest of SceneDelegate
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM