简体   繁体   中英

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

I need to do api call as soon as the app launched, based on the value return I should decide whether the user need to continue to use the app or it should be closed.. May I do that api call in app delegate.

You can show a splash screen with loading indicator and do the API call

Please check 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
}

the when you got the response from the API you can call splashImageView.removeFromSuperview() to let user use the app

I assume what you mean by AppDelegate is the didFinishLaunchingWithOptions function which is called when you open your application. It is mainly used for setting up the initial configuration of an application. Although there might be some exceptions, It should not wait an async operation to continue. What you can do is, for an entry point, creating a view controller (or view) as a waiting state, and making API calls in there. After getting the response, you can navigate into detailed pages of your app, or show user an informative popup and close the app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM