IOS 当 currentUser() != nil 时解析主屏幕

IOS Parse Homescreen when currentUser() != nil

我正在努力解决这个问题(看起来很简单),但我认为这可能是因为我的 login/signup VC 在导航控制器中,而我的应用程序的其余部分(主屏幕等)在一个单独的TabBarController.

我的 "Is Initial VC" 设置为保存我的注册和登录 VC 的导航控制器,这启动并完美运行,我能够登录并转到我的 HomeVC,如下所示:

func transitionToHome() {
    let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController)
    view.window?.rootViewController = homeViewController
    view.window?.makeKeyAndVisible()
}

在我的 AppDelegate 内部,我有以下代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    setupParse()

    if  PFUser.current() != nil {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let homeVC = storyboard.instantiateViewController(identifier: Constants.Storyboard.homeViewController)
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController?.present(homeVC, animated: true, completion: nil)
    }

    return true
}

我也向后尝试过(Home TabbarVC 是 Initial,如果 Pf.current() == nil,在 app delegate 中,使用登录启动),并确保 PFUser.current() 是注销后设置为零,但它仍然对我不起作用。我已经阅读了其他类似的问题,但我认为我的问题可能是标签栏与导航 - 或者使用 windows 的问题。先谢谢你了。

您需要将代码移到 SceneDelegate 中,并且需要对其进行一些更改:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }

    if  PFUser.current() != nil {
        window = UIWindow(windowScene: windowScene)
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let homeVC = storyboard.instantiateViewController(identifier: Constants.Storyboard.homeViewController)
        window?.rootViewController = homeVC
        self.window?.makeKeyAndVisible()
    }

}