【问题标题】:Is it good method to call api in app delegate in iOS?在iOS的应用程序委托中调用api是一种好方法吗?
【发布时间】:2020-07-03 10:52:56
【问题描述】:

我需要在应用程序启动后立即进行 api 调用,根据返回的值我应该决定用户是需要继续使用应用程序还是应该将其关闭.. 我可以在应用程序委托中进行该 api 调用吗.

【问题讨论】:

    标签: ios


    【解决方案1】:

    您可以显示带有加载指示器的初始屏幕并进行 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() 让用户使用该应用程序

    【讨论】:

    • 我该怎么做?应用程序委托将被调用
    • @HariharasudhanJ,我已经用一个例子编辑了答案
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      相关资源
      最近更新 更多