비전공자 개발일기

Log Out 본문

SWIFT

Log Out

HiroDaegu 2023. 1. 16. 23:17
728x90
SMALL

 

로그인 후 화면
실행 화면

    import UIKit
    import FirebaseAuth

    class ProfileViewController: UIViewController {
        
        @IBOutlet var tableView: UITableView!
        
        let data = ["Log Out"]
        
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
            tableView.delegate = self
            tableView.dataSource = self
        }
        
    }

    extension ProfileViewController: UITableViewDelegate, UITableViewDataSource {
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return data.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.text = data[indexPath.row]
            cell.textLabel?.textAlignment = .center
            cell.textLabel?.textColor = .red
            return cell
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
            
            let actionSheet = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
            actionSheet.addAction(UIAlertAction(title: "Log Out", style: .destructive, handler: { [weak self] _ in
                guard let strongSelf = self else { return }
                do {
                    try FirebaseAuth.Auth.auth().signOut()
                    
                    let vc = LoginViewController()
                    let nav = UINavigationController(rootViewController: vc)
                    nav.modalPresentationStyle = .fullScreen
                    strongSelf.present(nav, animated: true)
                } catch  {
                    print("Failed to log out")
                }
            }))
            actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))
            
            present(actionSheet, animated: true)
            
            
        }
        
    }
728x90
LIST

'SWIFT' 카테고리의 다른 글

Random Chinese character Generator  (0) 2023.01.29
Memo App made by SwiftUI  (0) 2023.01.17
Firebase Auth(Email / Password Login)  (0) 2023.01.15
Choose a picture in iPhone's photo library(UIImagePickerController)  (0) 2023.01.14
MVC Pattern  (0) 2023.01.13