1

我想实现自动登录功能。当我打开我的应用程序时,它将转到“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

}

现在我想去登录视图控制器,如果我要注销应用程序。但由于我的 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)
4

2 回答 2

1

所有代码都来自我的项目

1.在哪里设置 Keywindow RootViewController

let navigator = Container.shared.resolve(INavigator.self)!
let localStorageService = Container.shared.resolve(ILocalStorageService.self)!

// if you store login user key or not
if localStorageService.isKeyExist(key: Configs.KEY_USER) {

    // if there is key,then goes to main page
    navigator.switchRoot(keyWindow: window!, scene: .main(viewModel: MainTabBarViewModel()))
} else {
    // there is no key,then goes to login page
    navigator.switchRoot(keyWindow: window!, scene: .intro(viewModel: IntroViewModel()))
}

2.通过调用Api登录页面,可以从api获取一个token(key),这个token是后端开发者高度定制的,请将此token(key)保存在用户默认中。

3. 稍后当您需要在您的应用程序中调用某些授权 API 时,您将需要此令牌。

于 2020-06-10T11:01:53.187 回答
0

您似乎缺少 的初始化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()
    }
}
于 2020-06-09T19:37:39.210 回答