iOS task when application is closed

i0S Swift Issue

Question or problem in the Swift programming language:

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.

How to solve the problem:

Solution 1:

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 also.

Solution 2:

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.

Solution 3:

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

Solution 4:

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

Hope this helps!