Closed jayn2u closed 6 months ago
Modal
Scene
rootViewController
MainViewController()
// // ViewController.swift // Week4_Mission // // Created by Jiwoong CHOI on 5/10/24. // import SnapKit import UIKit import FirebaseAuth class MainViewController: UIViewController { // MARK: - Properties let emailTextField: UITextField = { let textField = UITextField() textField.placeholder = "이메일을 입력하세요" textField.borderStyle = .roundedRect return textField }() let passwordTextField: UITextField = { let textField = UITextField() textField.placeholder = "비밀번호를 입력하세요" textField.borderStyle = .roundedRect textField.isSecureTextEntry = true return textField }() let loginButton: UIButton = { let button = UIButton() button.setTitle("로그인", for: .normal) button.backgroundColor = .systemBlue button.layer.cornerRadius = 5 return button }() let signUpButton: UIButton = { let button = UIButton() button.setTitle("회원가입", for: .normal) button.backgroundColor = .systemBlue button.layer.cornerRadius = 5 return button }() // MARK: - Life Cycle override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .systemBackground loginButton.addTarget(self, action: #selector(loginButtonTapped), for: .touchUpInside) signUpButton.addTarget(self, action: #selector(signupButtonTapped), for: .touchUpInside) setupViews() setupLayout() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) } // MARK: - Helpers func setupViews() { view.addSubview(emailTextField) view.addSubview(passwordTextField) view.addSubview(loginButton) view.addSubview(signUpButton) } func setupLayout() { emailTextField.snp.makeConstraints { make in make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(50) make.left.equalTo(view).offset(20) make.right.equalTo(view).offset(-20) } passwordTextField.snp.makeConstraints { make in make.top.equalTo(emailTextField.snp.bottom).offset(20) make.left.right.equalTo(emailTextField) } loginButton.snp.makeConstraints { make in make.top.equalTo(passwordTextField.snp.bottom).offset(20) make.left.right.equalTo(emailTextField) make.height.equalTo(50) } signUpButton.snp.makeConstraints { make in make.top.equalTo(loginButton.snp.bottom).offset(20) make.left.right.equalTo(emailTextField) make.height.equalTo(50) } } @objc func loginButtonTapped() { guard let inputEmailString = emailTextField.text, let inputPasswordString = passwordTextField.text else { return } Auth.auth().signIn(withEmail: inputEmailString, password: inputPasswordString) { authResult, error in if let error = error { debugPrint("Error occured in signIn process") debugPrint("Error message below here") debugPrint(error.localizedDescription) } guard let authResult = authResult else { return } debugPrint(authResult) let profileLoginedViewController = ProfileLoginedViewController() profileLoginedViewController.emailLabel.text = inputEmailString // 이 부분이 핵심이야! guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else { return } sceneDelegate.window?.rootViewController = profileLoginedViewController } } @objc func signupButtonTapped() { guard let emailString = emailTextField.text, let passwordString = passwordTextField.text else { return } Auth.auth().createUser(withEmail: emailString, password: passwordString) { authResult, error in if let error = error { debugPrint("Error occured in signUp process...!") debugPrint("Error message below here") debugPrint(error.localizedDescription) } guard let authResult = authResult else { return } debugPrint(authResult) } } } // MARK: - Preview #if canImport(SwiftUI) && DEBUG import SwiftUI struct MainViewController_Preview: PreviewProvider { static var previews: some View { ViewControllerRepresentable().edgesIgnoringSafeArea(.all) } } struct ViewControllerRepresentable: UIViewControllerRepresentable { func makeUIViewController(context: Context) -> MainViewController { return MainViewController() } func updateUIViewController(_ uiViewController: MainViewController, context: Context) { // leave this empty } } #endif
// // ProfileLoginedViewController.swift // Week4_Mission // // Created by Jiwoong CHOI on 5/10/24. // import SnapKit import UIKit class ProfileLoginedViewController: UIViewController { // MARK: - UI Components let emailLabel: UILabel = { let label = UILabel() return label }() let logoutButton: UIButton = { let button = UIButton() button.setTitle("로그아웃", for: .normal) button.setTitleColor(.systemBlue, for: .normal) return button }() // MARK: - Life Cycle override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .systemBackground setupViews() setupLayouts() logoutButton.addTarget(self, action: #selector(logoutButtonTapped), for: .touchUpInside) } // MARK: - Helpers private func setupViews() { view.addSubview(emailLabel) view.addSubview(logoutButton) } private func setupLayouts() { emailLabel.snp.makeConstraints { make in make.center.equalToSuperview() } logoutButton.snp.makeConstraints { make in make.top.equalTo(emailLabel.snp.bottom).offset(20) make.centerX.equalTo(emailLabel) } } // MARK: - Button Actions @objc func logoutButtonTapped() { debugPrint("Logout success...!") // 이 부분을 핵심이야! guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else { return } sceneDelegate.window?.rootViewController = MainViewController() } }
JWT
Firebase
UserDefaults
Scene 전환 간 메모리 핸들링
질문 설명
Modal
형태로 뷰를 추가하던 것과 달리 아예Scene
의 rootView를 바꾸는 방법은 어떨까 싶어서 시도해본 코드야.rootViewController
에 View 객체를 생성하고 연결하는 코드로 작성이 되어있어.MainViewController()
가 아니라 새로 만들어진MainViewController()
라고 생각해.MainViewController()
가 무수히 생겨나서 메모리에 안좋은 것이 아닌가 싶어. 그리고 iOS에서 자동적으로 사용하지 않는 메모리를 해지하는 것도 궁금하구.구현화면
코드
MainViewController.swift
ProfileLoginedViewController.swift