Question: I am building an app which uploads files to the server via ajax.
The problem is that users most likely sometimes won't have the internet connection and client would like to have the ajax call scheduled to time when user have the connection back. This is possible that user will schedule the file upload when he's offline and close the app. Is it possible to do ajax call when the application is closed (not in background)?
When app is in the background, we can use background-fetch but I have never faced a problem to do something when app is closed.

Answer: 
```
- (void)applicationWillTerminate:(UIApplication *)application {
    if (application) {
        __block UIBackgroundTaskIdentifier backgroundTask  = UIBackgroundTaskInvalid;
        backgroundTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
            backgroundTask = UIBackgroundTaskInvalid;
        }];
        [self doSomething];
        [application endBackgroundTask:backgroundTask];
        backgroundTask = UIBackgroundTaskInvalid;
    }
}

```
I have already answered a similar question: 
<https://stackoverflow.com/a/57245917/6157415>

You can implement notification, when user receive notification part of your code can be executed.

in particular there are **Silent Push Notification** to do this:

> 
> Sometimes, you may want to use a **Silent Push Notification** to update
>  content inside you app in the background. A silent push notification
>  is defined as a push that does not have an alert, badge or sound, and
>  just has Key-Value data.
> 
> 
> 

From: <https://docs.mobile.sailthru.com/docs/silent-push-notifications>