我想实现自动登录功能。当我打开我的应用程序时,它将转到“ loginViewController”。登录后,我将登录ID保存为UserDefaults。如果UserDefaults具有该值,则第二次将ift转到“ SlideMenuController”。我已经在“ sceneDelegate”中编写了代码。代码是-
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let logId = UserDefaults.standard.value(forKey: "loginId") {
goToPlayerMenu()
}
guard let _ = (scene as? UIWindowScene) else { return }
}
func goToPlayerMenu() {
let storyboard = UIStoryboard(name: "PlayerHomeScreen", bundle: nil)
let homeViewController = storyboard.instantiateViewController(withIdentifier: "PlayerHomeViewController") as! PlayerHomeViewController
let menuViewController = storyboard.instantiateViewController(withIdentifier: "PlayerMenuViewController") as! PlayerMenuViewController
let nvc: UINavigationController = UINavigationController(rootViewController: homeViewController)
nvc.navigationBar.barTintColor = UIColor.black
nvc.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
nvc.navigationItem.leftBarButtonItem?.tintColor = .white
nvc.navigationBar.tintColor = UIColor.black
//menuViewController.homeViewController = nvc
let slideMneu = SlideMenuController.init(mainViewController: nvc, leftMenuViewController: menuViewController)
slideMneu.delegate = homeViewController as? SlideMenuControllerDelegate
slideMneu.changeLeftViewWidth(UIScreen.main.bounds.width-40)
slideMneu.automaticallyAdjustsScrollViewInsets = true
self.window?.rootViewController = slideMneu
}
现在,如果我要注销该应用程序,我想转到登录viewcontroller。但是,由于我的rootViewController是“ slideMneu”。我无法转到“ loginViewController”。注销后是否有任何选项可以转到“ loginViewController”?
我的注销功能是-
func logout(){
UserDefaults.standard.removeObject(forKey: "customer_id")
UserDefaults.standard.removeObject(forKey: "isContributor")
FUser.logOutCurrentUser { (logout) in
if logout == true{
print("DEBUG: Logout completed")
}
}
let Alert = UIAlertController(title:Common.sharedInstance().TITLE_ALERT, message:Common.sharedInstance().CONFIRM_ALERT, preferredStyle: .alert)
let OKButtonAction = UIAlertAction(title: Common.sharedInstance().ALERT_YES, style: UIAlertAction.Style.default) { (action:UIAlertAction!) in
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let LoginViewController = storyBoard.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
self.navigationController?.pushViewController(LoginViewController!, animated: true)
// self.navigationController?.popViewController(animated: true)
}
let Cancel = UIAlertAction(title:Common.sharedInstance().NO, style: .default, handler: nil)
Alert.addAction(OKButtonAction)
Alert.addAction(Cancel)
self.present(Alert, animated: true, completion:nil)
您似乎缺少window
的初始化,可以通过以下方式对其进行修复:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
if let logId = UserDefaults.standard.value(forKey: "loginId") {
goToPlayerMenu()
}
}