gpt4 book ai didi

ios - 使用 Firebase 在 iOS 13.2 中登录导航/Segue

转载 作者:行者123 更新时间:2023-11-29 05:20:39 25 4
gpt4 key购买 nike

我正在尝试使用 Firebase 为我的 iOS 13.2 应用实现SignIn with Google。用户登录后,如何实现从登录页面到主屏幕(ViewController)的segue

有一个通过 GIDSignInDelegate 附加到 AppDelegate 的方法,它会在用户登录时通知我们。我想在此时segue到主屏幕。此代码位于 AppDelegate 中,我无法使用 AppDelegate 的 windowStoryBoard 加载,因为新的 SceneDelegate 行为。

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 来更改 Root View 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
}

关于ios - 使用 Firebase 在 iOS 13.2 中登录导航/Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58712882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com