備忘録
firebaseのセットアップおよびユーザー登録機能の開発は完了している前提
AppDelegate
変数window
を追加し、func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
を下記のように修正する。
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
// 現在のユーザーを取得
if Auth.auth().currentUser != nil {
// ログインしている場合は、メイン画面に遷移
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewController(withIdentifier: "MainView")
window?.rootViewController = mainViewController
} else {
// ログインしていない場合は、ログイン画面に遷移
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
window?.rootViewController = loginViewController
}
return true
}
SceneDelegate
変数window
を追加し、func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
を下記のように修正する。
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 }
// 現在のユーザーを取得
if Auth.auth().currentUser != nil {
// ログインしている場合は、メイン画面に遷移
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewController(withIdentifier: "MainView")
window?.rootViewController = mainViewController
} else {
// ログインしていない場合は、ログイン画面に遷移
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
window?.rootViewController = loginViewController
}