首页 > 解决方案 > 登录视图控制器不想在登录后关闭

问题描述

我想问我有一个登录视图控制器,在正确添加电子邮件和密码后不想关闭。但是当我第一次尝试模拟器时,登录工作正常并指向我的家庭控制器,但在我退出之后。并尝试再次登录,然后登录不会关闭我的登录视图控制器。这怎么可能?起初是工作,后来不工作,在这里我向您展示我的代码。

// this is my sign out button
    @objc private func handleSignOut() {
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Log Out".localized(), style: .destructive, handler: { (_) in

            self.progressHUD.show(in: self.view)
            ProfileServices.shared.signOutUser { success in

                if success {
                    self.progressHUD.dismiss(animated: true)
                    let signInVC = SigninViewController()
                    self.present(signInVC, animated: true, completion: nil)
                } else {
                    self.progressHUD.textLabel.text = "Error"
                    self.progressHUD.dismiss(afterDelay: 0.4)
                }
            }
        }))

        alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
        present(alert, animated: true, completion: nil)
    }

// this is my sign out function in ProfileServices.shared
    func signOutUser(completion: @escaping (Bool) -> Void) {
        AF.request(API_URL.AUTHENTICATION.LOGOUT, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: HEADERS, interceptor: nil).responseData { (dataResponse) in

            if dataResponse.error == nil {
                let domain = Bundle.main.bundleIdentifier!
                UserDefaults.standard.removePersistentDomain(forName: domain)
                UserDefaults.standard.synchronize()
                UserDefaults.removeToken()

                completion(true)
            } else {
                completion(false)
            }
        }
    }

// this is my sign in route in my sign in view controller
    func routeToMainView(_ data: SigninModel.Response) {
        let school = UserDefaults.getSelectedSchool()
        guard let schools = data.schools?.schools else { return }

        if let selectedSchool = school, let selected = schools.first(where: { $0.id == selectedSchool.id}) {
            UserDefaults.saveSelectedSchool(data: selected)
            let vc = MainViewController()
            self.viewController?.navigationController?.setViewControllers([vc], animated: true)
        } else {
            if schools.count > 1 {
                let vc = SwitchSchoolViewController()
                self.viewController?.navigationController?.setViewControllers([vc], animated: true)
            } else {
                guard let selected = schools.first else { return }
                UserDefaults.saveSelectedSchool(data: selected)
                DispatchQueue.main.async {
                    let vc = MainViewController()
                    self.viewController?.navigationController?.setViewControllers([vc], animated: true)
                }
            }
        }
    }

// this is in my appDelegate
    var root: UIViewController?
        root = SigninViewController()
        if UserDefaults.getToken() != nil {
            root = MainViewController()
        }

标签: iosswiftauthenticationaccess-token

解决方案


在注销时,您需要关闭呈现的 viewController。

到位 :

let signInVC = SigninViewController()
self.present(signInVC, animated: true, completion: nil)

你需要使用:

self.dismiss(animated: true, completion: nil)

或者如果您将使用 Push,则弹出。


推荐阅读