大佬教程收集整理的这篇文章主要介绍了如何在不询问iOS Swift登录的情况下自动登录?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
的截图
我的问题是当用户注销并返回时它自动进入主屏幕而不是登录屏幕[此问题是由于SWRevealViewController是初始视图控制器].
在viewDidAppear中检查firebase中是否有可用的用户
override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if API.User.CURRENT_USER != nil { // segue to the Tab Bar Controller self.performSegue(withIdentifier: "signIn@R_823_10586@bBar",sender: nil) } }
登录动作:
@IBACtion func SignInButton(_ sender: Any) { view.endEdiTing(true) guard let email = emailTextField.text,!email.isEmpty,let password = passwordTextField.text,!password.isEmpty else { self.showErrorAlert(message: "Username or email or passowrd should not be empty") return } // show the progress to the user ProgressHUD.show("StarTing sign-in...",interaction: falsE) // use the signIn class method of the Authservice class Authservice.signIn(email: emailTextField.text!,password: passwordTextField.text!,onsuccess: { // on success segue to the Tab Bar Controller API.User.observeCurrentUser { user in guard let currentUser = Auth.auth().currentUser else { return } Prefsmanager.sharedinstance.UIDfirebase = currentUser.uid Prefsmanager.sharedinstance.username = user.username! Prefsmanager.sharedinstance.userEmail = user.email! Prefsmanager.sharedinstance.imageURL = user.profileImageURL! ProgressHUD.showsuccess("Sucessfully signed in.") self.performSegue(withIdentifier: "signIn@R_823_10586@bBar",sender: nil) } },onError: { errorString in ProgressHUD.dismiss() self.showErrorAlert(message: errorString ?? "Server error") }) }
}
SWRevealViewController菜单表我列出了菜单,如家,预订,个人资料,注销:
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! MenuTableViewCell cell.menuName.text = menuName[indexPath.row] cell.menuIcon.image = UIImage(named: menuImage[indexPath.row]) return cell } func tableView(_ tableView: UITableView,didSELEctRowAt indexPath: IndexPath) { // tableView.deSELEctRow(at: indexPath,animated: truE) let row = indexPath.row if row == 0{ let storyboard = UIStoryboard(name: "Home",bundle: nil) let obj = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController let navController = UINavigationController(rootViewController: obj) navController.setViewControllers([obj],animated:truE) navController.tabBarController?.tabBar.isHidden = false self.revealViewController().setFront(navController,animated: truE) self.revealViewController().setFrontViewPosition(FrontViewPosition.left,animated: truE) } else if row == 1{ let storyboard = UIStoryboard(name: "Bookings",bundle: nil) let obj = storyboard.instantiateViewController(withIdentifier: "BookingsViewController") as! BookingsViewController let navController = UINavigationController(rootViewController: obj) navController.setViewControllers([obj],animated:truE) self.revealViewController().setFront(navController,animated: truE) } else if row == 2 { let storyboard = UIStoryboard(name: "Profile",bundle: nil) let obj = storyboard.instantiateViewController(withIdentifier: "profileViewController") as! profileViewController let navController = UINavigationController(rootViewController: obj) navController.setViewControllers([obj],animated: truE) } else if row == 3 { print(indexPath) // Log out user from Firebase Authservice.signOut(onsuccess: { // Present the Sign In VC // Prefsmanager.sharedinstance.logoutprefences() let storyboard = UIStoryboard(name: "Main",bundle: nil) let signInVC = storyboard.instantiateViewController(withIdentifier: "signInViewController") self.present(signInVC,animated: truE) // self.navigationController?.pushViewController(signInVC,animated: truE) }) { (errormessagE) in ProgressHUD.showError(errormessagE) } } }
在家庭视图控制器中,检查用户是否可用:
override func viewDidAppear(_ animated: Bool) { if API.User.CURRENT_USER != nil { // segue to the Tab Bar Controller self.performSegue(withIdentifier: "signIn@R_823_10586@bBar",sender: nil) } super.viewDidAppear(true) self.tabBarController?.tabBar.isHidden = false }
Authservice.signOut(onsuccess: { // Present the Sign In VC let storyboard = UIStoryboard(name: "Main",bundle: nil) let signInVC = storyboard.instantiateViewController(withIdentifier: "signInViewController") as! signInViewController let nav = UINavigationController(rootViewController:signInVC) let application = UIApplication.shared.delegate as! AppDelegate application.window!.rootViewController = nav }) { (errormessagE) in ProgressHUD.showError(errormessagE) }
现在在AppDelegate.swift文件中,在application(_:didFinishLaunchingWithOptions :)方法中,你必须检查用户是否为nil.
if API.User.CURRENT_USER != nil { let mainNav = self.window?.rootViewController as! UINavigationController let storyBoard = UIStoryboard(name: "tabBar",bundle:nil) let sWRevealViewController = storyBoard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController mainNav.pushViewController(sWRevealViewController,animated: falsE) }
以上是大佬教程为你收集整理的如何在不询问iOS Swift登录的情况下自动登录?全部内容,希望文章能够帮你解决如何在不询问iOS Swift登录的情况下自动登录?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。