在iOS中的应用程序委托中调用api是一种好方法吗?

时间:2020-07-03 10:52:56

标签: ios

我需要在应用程序启动后立即进行api调用,基于返回的值,我应该决定用户是否需要继续使用该应用程序,或者应该关闭该应用程序。我可以在应用程序委托中进行该api调用

2 个答案:

答案 0 :(得分:0)

您可以显示带有加载指示器的初始屏幕并进行API调用

请检查this answer

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    let splashImage = UIImage.autoAdjustNamed("Default.png")
    let splashImageView = UIImageView(image: splashImage)
    window.rootViewController?.view.addSubview(splashImageView)
    window.rootViewController?.view.bringSubviewToFront(splashImageView)
    UIView.animate(
        withDuration: 1.5,
        delay: 2.0,
        options: .curveEaseInOut,
        animations: {
            splashImageView.alpha = 0.0
            let x: CGFloat = -60.0
            let y: CGFloat = -120.0
            splashImageView.frame = CGRect(
                x: x,
                y: y,
                width: splashImageView.frame.size.width - 2 * x,
                height: splashImageView.frame.size.height - 2 * y)
        })

    return true
}

当您从API获得响应时,可以调用splashImageView.removeFromSuperview()以允许用户使用该应用程序

答案 1 :(得分:0)

我假设AppDelegate的意思是打开应用程序时调用的didFinishLaunchingWithOptions函数。它主要用于设置应用程序的初始配置。尽管可能会有一些例外,但它不应等待异步操作继续。对于入口点,您可以做的是将视图控制器(或视图)创建为等待状态,然后在其中进行API调用。收到响应后,您可以导航到应用程序的详细页面,或向用户显示信息丰富的弹出窗口并关闭应用程序。