テックキャンプ142日目〜ログインの有無で初期画面を変更

kobasaです(´ω`*)
アプリを起動したとき、ログイン状態ならログイン画面をスキップして
アプリ画面に遷移する設定の基本を勉強していました。
便利ですよね!「ログイン状態を保持する」ボタンも作りたいなぁ。

142日目の勉強内容

SceneDelegateファイルに記述する。
Firebaseのユーザー管理を使用するので、import FirebaseAuth

func scene(_scene: UIScene, willConnectTosession: UISceneSession, optionsconnectionOptions: UIScene.ConnectionOptions) {

	if Auth.auth().currentUser?.uid != nil{
	//ユーザーidが見つかったのでログイン画面をスキップし、回答投稿画面へ
	let window = UIWindow(windowScene: scene as! UIWindowScene)
	self.window= window
	window.makeKeyAndVisible()

	let storyBoard = UIStoryboard(name: "Main", bundle: nil)
	let viewController = storyBoard.instantiateViewController(withIdentifier: "viewVC")
	let navigationVC = UINavigationController(rootViewController: viewController)
	window.rootViewController= navigationVC

	}else{
	//ユーザーのidが見つからなかったのでログイン画面へ
	let window = UIWindow(windowScene: scene as! UIWindowScene)
	self.window= window
	window.makeKeyAndVisible()

	let storyBoard = UIStoryboard(name: "Main", bundle: nil)
	let viewController = storyBoard.instantiateViewController(withIdentifier: "loginVC")
	let navigationVC = UINavigationController(rootViewController: viewController)
	window.rootViewController= navigationVC
	}

	guardlet_ = (scene as? UIWindowScene) else{ return}
}

let window = の記述からlet storyBoard = の記述までは
重複してるので条件式の外にまとめても良さそう。

currentUserのユーザーIDがFIrebase上でログイン状態なら〜という条件式は
Railsのdeviseで似たような記述をしていましたね。懐かしい。
ナビゲーションビューを使用したときはこの記述なのかな?その辺りは注意した方が良さそう。

コメント

タイトルとURLをコピーしました