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

Posted

技术标签:

【中文标题】在应用程序启动之前执行完成处理程序【英文标题】:Performing a completion handler before app launches 【发布时间】:2019-11-13 20:45:04 【问题描述】:

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

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

在第 2 步中,如果保存了 UserDefaults,那么我需要使用带有完成处理程序的函数使用 Firebase 登录用户。如果是第 2 步,我想在完成块中打开 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 的视频,当我需要运行完成处理程序时,过渡到应用程序时并不顺畅(有短暂的一秒钟黑屏)。

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

【问题讨论】:

我认为authentificator.login() 是异步的? 对,你是对的 答案是应用启动后必须进行异步检查。空白屏幕是意料之中的,应用程序在等待结果时不知道该做什么。同时,您需要在进行身份验证检查时创建一个看起来不错的 UI。我正在模拟一些可能对你有帮助的东西。 【参考方案1】:

我不得不在我的 Firebase 应用程序中处理类似的情况。我通常做的是InitialViewController。这是始终加载的视图控制器,无论如何。此视图控制器最初设置为与启动屏幕完全一样无缝。

这是InitialViewController 在界面生成器中的样子:

这就是我的启动屏幕的样子:

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

因此,在此InitialViewController 中,您在viewDidAppear() 中进行身份验证检查。如果用户已登录,我们对主视图控制器执行 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()
        
       

【讨论】:

我喜欢这种方法。似乎是一个非常简单的解决方法。我会在我的应用中试一试。 如果有任何不妥之处,请告诉我。当应用程序启动时,我一直需要立即处理异步检查。经过几个月的开发,我已经确定了这样的事情(虽然有点复杂)。您似乎对 ios 开发足够熟悉,能够弄清楚当用户退出您的应用时如何使相同的视图控制器正常工作。 另请注意:我刚刚意识到我的两个 gif 图像/它们的场景不匹配。他们现在应该更有意义了。 我让它工作了。我什至在 Twitter 风格的动画中添加了 RevealingSplashView,为了简单起见,我省略了它。再次感谢您的帮助。 gph.is/g/EJg8v0Q。非常流畅(这是用户已经登录的情况)

以上是关于在应用程序启动之前执行完成处理程序的主要内容,如果未能解决你的问题,请参考以下文章

SCNAction 完成处理程序等待手势执行

在MySQL中执行启动事件的程序挂钩之前,是否可以建立连接?

centos6之前版本的启动流程

完成处理程序调用太晚

main函数执行以前还会执行啥代码

main函数执行以前还会执行啥代码