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: 
The short answer is no, your app can't run code after being terminated.

You can run code in your `AppDelegate`'s `applicationWillTerminate`, but that method is mainly intended for saving user data and other similar tasks.

See [this answer](https://stackoverflow.com/a/32225247/5389870) also.
Yes you can do stuff in the background. You are limited to several different background modes of execution. Server communication is one of the modes that is allowed (background fetch). Make sure you set the properties correctly in Xcode per the guidelines (i.e. don't say you are a audio app when you are doing data transfer). See the details here:

<https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html>

I found your question because it has the Cordova tag associated with it. If you are using Cordova you can use this plugin here to manage things in the background:

<https://github.com/katzer/cordova-plugin-background-mode>

Edit: If the user is FORCE closing / terminating the app then there is nothing you can do. If they are just exiting the app to the home screen and use other apps, then you can run in the background.

The other option you can do is schedule a local notification to upload the file. If you app successfully uploads the file because it is open / has a connection / did it in the background, then you cancel the local notification.

If the local notification isn't cancelled it will remind the user the file is not uploaded and when they open the notification your app will start back where it left off.