gpt4 book ai didi

swift - 在应用程序启动之前执行完成处理程序

转载 作者:行者123 更新时间:2023-11-28 05:35:24 25 4
gpt4 key购买 nike

我正尝试通过以下两种方式之一打开应用:

  1. 如果用户没有保存UserDefaults,则打开一个WelcomeViewController
  2. 如果用户保存了UserDefaults,则打开一个MenuContainerViewController作为主页

在第 2 步中,如果保存了 UserDefaults,那么我需要使用 Firebase 登录用户,我通过一个带有完成处理程序的函数。如果第 2 步是这种情况,我想在完成 block 中打开 MenuContainerViewController,而不会出现任何 UI 问题。

这是我目前的代码:

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

self.window = UIWindow(frame: UIScreen.main.bounds)

FirebaseApp.configure()

guard
let email = UserDefaults.standard.string(forKey: "email"),
let password = UserDefaults.standard.string(forKey: "password")
else {
// User has no defaults, open welcome screen
let welcomeViewController = WelcomeViewController()
self.window?.rootViewController = welcomeViewController
self.window?.makeKeyAndVisible()
return true
}

// User has defaults saved locally, open home screen of app
let authentificator = Authentificator()
authentificator.login(with: email, password) { result, _ in
if result {
let menuContainerViewController = MenuContainerViewController()
self.window?.rootViewController = menuContainerViewController
self.window?.makeKeyAndVisible()
}
}

return true
}

这是当前 UI 的视频,当我需要运行完成处理程序时,过渡到应用程序时并不平滑(有一秒钟黑屏)。

https://gph.is/g/Zdbjq81

请帮我弄清楚如何让应用程序顺利启动。

最佳答案

我不得不在我的 Firebase 应用程序中处理类似的情况。我通常做的是制作一个 InitialViewController。这是始终加载的 View Controller ,无论如何。这个 View Controller 最初被设置为无缝地看起来与启动屏幕完全一样。

这就是 InitialViewController 在界面构建器中的样子: enter image description here

这是我的启动屏幕的样子: enter image description here

所以当我说它们看起来完全一样时,我的意思是它们看起来完全一样。此 InitialViewController 的唯一目的是处理此异步 检查并决定下一步要做什么,同时看起来像启动屏幕。您甚至可以在两个 View Controller 之间复制/粘贴界面构建器元素。

因此,在此 InitialViewController 中,您在 viewDidAppear() 中进行身份验证检查。如果用户已登录,我们将对主视图 Controller 执行 segue。如果没有,我们将动画化用户入职元素到位。演示我的意思的 gif 非常大(维度方面和数据方面),因此它们可能需要一些时间才能加载。您可以在下面找到每一个:

User previously logged in.

User not previously logged in.

这就是我在 InitialViewController 中执行检查的方式:

@IBOutlet var loginButton: UIButton!
@IBOutlet var signupButton: UIButton!
@IBOutlet var stackView: UIStackView!
@IBOutlet var stackViewVerticalCenterConstraint: NSLayoutConstraint!

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

//When the view appears, we want to check to see if the user is logged in.
//Remember, the interface builder is set up so that this view controller **initially** looks identical to the launch screen
//This gives the effect that the authentication check is occurring before the app actually finishes launching
checkLoginStatus()
}

func checkLoginStatus() {
//If the user was previously logged in, go ahead and segue to the main app without making them login again

guard
let email = UserDefaults.standard.string(forKey: "email"),
let password = UserDefaults.standard.string(forKey: "password")
else {
// User has no defaults, animate onboarding elements into place
presentElements()
return
}

let authentificator = Authentificator()
authentificator.login(with: email, password) { result, _ in
if result {
//User is authenticated, perform the segue to the first view controller you want the user to see when they are logged in
self.performSegue(withIdentifier: "SkipLogin", sender: self)
}
}
}

func presentElements() {

//This is the part where the illusion comes into play
//The storyboard elements, like the login and signup buttons were always here, they were just hidden
//Now, we are going to animate the onboarding UI elements into place
//If this function is never called, then the user will be unaware that the launchscreen was even replaced with this view controller that handles the authentication check for us

//Make buttons visible, but...
loginButton.isHidden = false
signupButton.isHidden = false

//...set their alpha to 0
loginButton.alpha = 0
signupButton.alpha = 0

//Calculate distance to slide up
//(stackView is the stack view that holds our elements like loginButton and signupButton. It is invisible, but it contains these buttons.)
//(stackViewVerticalCenterConstraint is the NSLayoutConstraint that determines our stackView's vertical position)
self.stackViewVerticalCenterConstraint.constant = (view.frame.height / 2) + (stackView.frame.height / 2)

//After half a second, we are going to animate the UI elements into place
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
UIView.animate(withDuration: 0.75) {
self.loginButton.alpha = 1
self.signupButton.alpha = 1

//Create correct vertical position for stackView
self.stackViewVerticalCenterConstraint.constant = (self.view.frame.height - self.navigationController!.navigationBar.frame.size.height - self.signupButton.frame.maxY - (self.stackView.frame.size.height / 2)) / 3
self.view.layoutIfNeeded()
}
}
}

关于swift - 在应用程序启动之前执行完成处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58845347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com