繁体   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