How to save NSTimer when entering the application?

I am here because I could not find a solution to my problem :(

I am making a simple application in which I have to send (via a socket) some information to the server (e.g. GPS l / L, accuracy, battery level, etc.).

The current code works great when the application is in the foreground.

myTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target:self selector: @selector(sendPosToServer:) userInfo:nil repeats: YES]; myTimer2 = [NSTimer scheduledTimerWithTimeInterval: 3.0 target:self selector: @selector(sendBatteryToServer:) userInfo:nil repeats: YES]; myTimer3 = [NSTimer scheduledTimerWithTimeInterval: 5.0 target:self selector: @selector(sendResToServer:) userInfo:nil repeats: YES]; myTimer4 = [NSTimer scheduledTimerWithTimeInterval: 5.0 target:self selector: @selector(sendQResToServer:) userInfo:nil repeats: YES]; myTimer5 = [NSTimer scheduledTimerWithTimeInterval: 3.0 target:self selector: @selector(sendPrecisionToServer:) userInfo:nil repeats: YES]; 

All thoses methods are called.

But when the application is entered in the background, all timers are invalid ... I read that iOS automatically stops the timers. I am looking for a way to call methods and send data when the application is in the background.

I need your help:)

Thanks everyone!

+8
ios background scheduling nstimer foreground
source share
3 answers

You need to read the manual for completing tasks in the background:

Background execution and multitasking

Here is my applicationDidEnterBackground for one of my applications. When I put it in the background, it does some disk cache maintenance:

 - (void)applicationDidEnterBackground:(UIApplication *)application { //As we are going into the background, I want to start a background task to clean up the disk caches if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking IE iOS 4 if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance __block UIBackgroundTaskIdentifier background_task; //Create a task object background_task = [application beginBackgroundTaskWithExpirationHandler: ^{ [application endBackgroundTask:background_task]; //Tell the system that we are done with the tasks background_task = UIBackgroundTaskInvalid; //Set the task to be invalid //System will be shutting down the app at any point in time now }]; //Background tasks require you to use asyncrous tasks dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Perform your tasks that your application requires //I do what i need to do here.... synchronously... [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform background_task = UIBackgroundTaskInvalid; //Invalidate the background_task }); } } 

}

+11
source share

From Apple docs:

Implementing long-term background tasks For tasks that require more execution time to complete, you must request specific permissions to run them in the background without pausing them. On iOS, only certain types of applications are allowed to run in the background:

  • Applications that play audio content for the user in the background, for example, an application for a music player.
  • Applications that constantly inform users of their location, such as a navigation application
  • Voice over Internet Protocol (VoIP) Applications
  • Press applications to download and process new content.
  • Applications regularly receiving updates from external accessories

If your application does not fall into one of these categories (and it seems that this is not so), then it will not be able to work in the background.

+5
source share

The iOS Application Programming Guide lists actions for which your application can remain active in the background. If you plan to distribute your application through the application store, your application will have to perform one of the following actions. If you just write something for yourself, perhaps you can combine your functionality with one of the allowed actions.

+4
source share

All Articles