注册成功后导航到主应用程序,我应该使用SceneDelegate吗? [英] Navigate to main app after successful signup, should I use SceneDelegate?

查看:22
本文介绍了注册成功后导航到主应用程序,我应该使用SceneDelegate吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 Xcode(使用 Swift 4 的 xcode 11.4)进行移动开发的新手,目前正在学习如何创建成功时导航到 MainViewController 的身份验证页面.

I am new to mobile development using Xcode (xcode 11.4 with Swift 4), and am currently learning how to create an authentication page that navigates to MainViewController on success.

我的故事板草图如下所示:

My story board sketch looks like this:

其中顶行是 LoginViewController 转入 ConfirmationViewController.底行是应用的 MainViewController,用户创建帐户后即可访问.

Where the top row is the LoginViewController segueing into ConfirmationViewController. The bottom row is the MainViewController of the app, accessible after the user has created an account.

在我的 SceneDelegate.swift 中,如果用户未登录,我将入口点设置为 LoginViewController,否则入口点为 MainViewController>.这里的代码片段:

In my SceneDelegate.swift, I set the entry point to be LoginViewController if user is not signed in, otherwise the entry point is MainViewController. Code snippet here:

    var window: UIWindow?

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 }

    // initialize application window and scenes
    self.window = UIWindow(windowScene: windowScene)
    let errorViewController   = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "ErrorPage")
    let tabViewController     = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "Tab"  )
    let signUpController      = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "SignUp")

    // initialize storyboard entry point
    AWSMobileClient.default().initialize { (userState, error) in
            if let userState = userState {

            print("userState: \(userState)")

            switch (userState) {
                case .guest:
                    print("user is in guest mode.")
                    self.window?.rootViewController = signUpController
                case .signedOut:
                    print("user signed out")
                    self.window?.rootViewController = signUpController
                case .signedIn:
                    print("user is signed in")
                    self.window?.rootViewController = tabViewController
                case .signedOutUserPoolsTokenInvalid:
                    print("need to login again.")
                    self.window?.rootViewController = signUpController
                case .signedOutFederatedTokensInvalid:
                    print("user logged in via federation, but currently needs new tokens")
                    self.window?.rootViewController = signUpController
                default:
                    print("unsupported")
            }

            } else if let error = error {
            print("error: AWSMobileClient failed to load user \(error)")
            self.window?.rootViewController = errorViewController
        }
    }
}

这按预期工作.现在,在我的 ConfirmationViewController 中,我需要一些逻辑,在成功确认后将用户引导至主应用 MainViewController

This works as intended. Now in my ConfirmationViewController I need to have some logic that directs the user to the main app MainViewController on successful confirmation

    @IBAction func onPressConfirmEmail(_ sender: UIButton) {
// @use: on confirmation success, navigate to application main
// @error: show error in the same page and ask person to try again

}

}

问题是我不确定这样做的正确"方法是什么.Xcode 设置项目的方式看起来非常具体,并且有一种正确的方式".请说明.

The problem is I'm not sure what is the "right" way to do this. The way Xcode has setup the project appears very specific, and there is "one right way." Please elucidate.

推荐答案

您可以在 Storyboard 中的 ConfirmationViewController 和 MainViewController 之间创建一个 Segue,然后您可以使用:

You can create a Segue between your ConfirmationViewController and your MainViewController in the storyboard and then you can use:

performSegue(withIdentifier: "segueIdentifier", sender: self).

或者你可以使用:

_ = navigationController?.popViewController(animated: true)

如果你想回到根视图控制器

If you want to go back to the root view controller

_ = navigationController?.popToRootViewController(animated: true)

这篇关于注册成功后导航到主应用程序,我应该使用SceneDelegate吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆