IOS task when closing the application

I am creating an application that uploads files to a server via ajax. The problem is that users will most likely sometimes not have an Internet connection, and the client would like the ajax call to be scheduled at a time when the user has a connection. Perhaps the user will pay to download the file when it disconnects and closes the application. Is it possible to make an ajax call when the application is closed (not in the background)? When the application is in the background, we can use background-fetch, but I never encountered a problem to do something when the application is closed.

+8
source share
4 answers

The short answer is no, your application cannot run the code after it completes.

You can run the code in the AppDelegate applicationWillTerminate , but this method is mainly designed to save user data and other similar tasks.

See this answer .

+6
source

Yes, you can do something in the background. You are limited to several different background execution modes. Communication with the server is one of the modes that are allowed (background selection). Make sure that you have correctly configured the properties in Xcode in accordance with the recommendations (i.e. do not say that you are an audio application when transferring data). See here for more details:

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

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

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

Edit: If the user closes / closes the FORCE application, then you can do nothing. If they just exit the application on the main screen and use other applications, you can work in the background.

Another option you can do is schedule a local notification to download the file. If the application successfully downloads the file because it is open / has a connection / did it in the background, you will cancel the local notification.

If the local notification is not canceled, it will remind the user that the file is not downloaded, and when they open the notification, your application will start working where it was stopped.

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

I already answered a similar question: fooobar.com/questions/1001315 / ...

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

in particular, there is Silent Push Notification for this:

Sometimes you can use Silent Push Notification to update content inside your application in the background. A silent push notification is defined as a push that does not have a warning, icon or sound, and simply has Key-Value data.

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

0
source

All Articles