簡體   English   中英

在 iOS 的應用程序委托中調用 api 是一種好方法嗎?

[英]Is it good method to call api in app delegate in iOS?

我需要在應用程序啟動后立即調用 api,根據返回的值我應該決定用戶是需要繼續使用應用程序還是應該關閉它。我可以在應用程序委托中調用 api 嗎?

您可以顯示帶有加載指示器的啟動屏幕並執行 API 調用

請檢查這個答案

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()讓用戶使用該應用程序

我假設您所說的 AppDelegate 是在您打開應用程序時調用的didFinishLaunchingWithOptions function 。 它主要用於設置應用程序的初始配置。 雖然可能有一些例外,但它不應該等待異步操作繼續。 對於入口點,您可以做的是創建一個視圖 controller(或視圖)作為等待的 state,並在其中進行 API 調用。 收到響應后,您可以導航到應用程序的詳細頁面,或向用戶顯示信息豐富的彈出窗口並關閉應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM