I am trying to implement SignIn with Google for my iOS 13.2 app using Firebase . How do I implement a segue
from the LogIn Page to HomeScreen(a ViewController
) as soon as the User has signed in.
There is a method attached to AppDelegate
through GIDSignInDelegate
which informs us when the user has signed in. I want to segue
at that point to the home screen. This code is in the AppDelegate
, and I am not able to use AppDelegate's
window
to load from StoryBoard
because of the new SceneDelegate
behaviour.
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
// ...
}
}
}
Expected result: https://stackoverflow.com/a/27136455/6311566 but this is not working in iOS 13.2
OLD WAY
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let rootVC = window?.rootViewController
return true
}
This is because AppDelegate.swift doesn't have window property anymore. Now you must use SceneDelegate.swift to change root view controller. Like shown in this example:
WHAT TO DO NOW
Now, you'll be moving that code over to your app's SceneDelegate Swift file:
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.