Last active
May 24, 2022 06:06
-
-
Save yoonchulkoh/d0c211ad7c37597213c700bd94d0f1d5 to your computer and use it in GitHub Desktop.
AppRootController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// FirebaseSample | |
// | |
// Created by Yoonchul Koh on 2018/10/01. | |
// Copyright © 2018 Yoonchul Koh. All rights reserved. | |
// | |
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
var rootController = AppRootController() | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
window = UIWindow(frame: UIScreen.main.bounds) | |
window?.rootViewController = rootController | |
window?.backgroundColor = UIColor.white | |
window?.makeKeyAndVisible() | |
return true | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppRootController.swift | |
// FirebaseSample | |
// | |
// Created by Yoonchul Koh on 2018/02/17. | |
// Copyright © 2018 Yoonchul Koh. All rights reserved. | |
// | |
import UIKit | |
import FirebaseAuth | |
class AppRootController: UIViewController { | |
private var currentViewController: UIViewController? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
Auth.auth().addStateDidChangeListener { [weak self] (auth, user) in | |
guard let strongSelf = self else { return } | |
if user != nil { | |
strongSelf.showMainScreen() | |
} else { | |
strongSelf.showLoginScreen() | |
} | |
} | |
if let user = Auth.auth().currentUser { | |
print(user.email) | |
showMainScreen() | |
} else { | |
showLoginScreen() | |
} | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
} | |
func showMainScreen() { | |
let storyboard = UIStoryboard(name: "MainViewController", bundle: nil) | |
let vc = storyboard.instantiateInitialViewController()! | |
changeViewController(vc) | |
} | |
func showLoginScreen() { | |
let storyboard = UIStoryboard(name: "LoginViewController", bundle: nil) | |
let vc = storyboard.instantiateInitialViewController()! | |
changeViewController(vc) | |
} | |
private func changeViewController(_ vc: UIViewController) { | |
removeCurrentViewController() | |
setCurrentViewController(vc) | |
} | |
private func setCurrentViewController(_ vc: UIViewController) { | |
currentViewController = vc | |
addChildViewController(vc) | |
view.addSubview(vc.view) | |
didMove(toParentViewController: vc) | |
} | |
private func removeCurrentViewController() { | |
guard let currentViewController = currentViewController else { return } | |
currentViewController.willMove(toParentViewController: nil) | |
currentViewController.view.removeFromSuperview() | |
currentViewController.removeFromParentViewController() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment