Day#22 一直忘记实作的Logout

前言

好像没什麽前言可说XD
就是把logout功能做出来~~
如此一来我们就可以测试登入/注册功能是否能成功跳转到相对应的页面,同时也开始开发profile view controller的部分。

Logout

ProfileViewController

首先引入相关的libraries。

import UIKit
import FirebaseAuth

ProfileViewController

接着,因为我们希望profile view能显示一些设定、登出之类的功能,因此同样选用table view。
接着我们将这个sign out元件放入table view中,并将指派对象指定为自己。

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

Extension

刻意练习,先前都提过UIViewController没有实作指派的功能,因此我们将额外继承其他的class,并实作出以下3个必要的方法。

  1. table view的行数
  2. table view的内容
extension ProfileViewController: UITableViewDelegate, UITableViewDataSource {
    //numberofrow
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    //cellforrow
    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
    }

接着第三个,是点选该行之後的行为。
我们预期使用者点选sign out之後,firebase auth应该要能够收到已登出的讯息,一来使得该使用者无法在app内有行为、二来该使用者可以再次登入。

因此我们使用addAction,并在handler加入方法,通知firebase auth进行登出。

最後将画面跳转离开。

    //selectrow
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let actionSheet = UIAlertController(title: "Sure to sign out",
                                            message: "",
                                            preferredStyle: .actionSheet)
        
        actionSheet.addAction(UIAlertAction(title: "Sign 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 // if not-> pop up as a card
                                                    strongSelf.present(nav, animated: true)
                                                }
                                                catch {
                                                    print("Fail to log out")
                                                }
                                            }))
        
        actionSheet.addAction(UIAlertAction(title: "Cancel",
                                            style: .cancel,
                                            handler: nil))
        
        present(actionSheet, animated: true)
    }
}

结语

若上述内容有误或可以改进的部分,欢迎留言以及提出任何指教~
谢谢 (´・∀・`)


<<:  Day23_CSS语法6

>>:  Day 22 - NFS 与档案权限

[Day 7]想不到有梗的标题LA(後端篇)

今天我们来看一下Native Camp用户设定,看一下有没有遗漏什麽栏位没有设置的, 这边可以看到可...

Day01 - 缘起:怎麽了?为什麽?如何掌握过於自由的程序码?

“Any fool can write code that a computer can unde...

Day14 Gin and Go Mod

Background Goland从1.11版本起就开始导入了GO Module功能,这样也不需要再...

Angular#5 专案:路由 登入系统>>首页

Angular [目标] 进入系统>>登入>>首页 1. VSCode 撰写...

[Day 30] 从此人人都可以是tinyML食神

「食神归位!」,从天上传来一个声音,「你本来是掌管烧菜的神仙,因触犯天条,而被罚落凡间受三十六劫、七...