在主要的Storyboard中,只有两个用于登录和注册控制器.
这是tabbar storyBoard
的截图我的问题是当用户注销并返回时它自动进入主屏幕而不是登录屏幕[此问题是由于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: "signInToTabbar",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: "signInToTabbar",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: "signInToTabbar",sender: nil) } super.vIEwDIDAppear(true) self.tabbarController?.tabbar.isHIDden = false}解决方法 当您从应用程序注销时,您必须将signIn视图控制器设置为这样的根视图控制器.
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) }
注意:请在标识符名称和视图控制器名称中添加故事板ID和视图控制器名称.
总结以上是内存溢出为你收集整理的如何在不询问iOS Swift登录的情况下自动登录?全部内容,希望文章能够帮你解决如何在不询问iOS Swift登录的情况下自动登录?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)