Run the application for more than 10 minutes in the background

I am trying to keep an iOS application active for more than 10 minutes when it enters the background state.

How can I implement this.

+44
ios background-process
Mar 16 2018-12-12T00:
source share
7 answers

See the Background Run section of iPhoneAppProgrammingGuide . In short, your application must be one of the following types:

  • 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

And you should add the following to Info.plist: Add the UIBackgroundModes key to your Info.plist and set its value to an array containing one or more of the following lines:

  • audio - the application plays audio content to the user in the background. (This content includes streaming audio or video content using AirPlay.)
  • location - the application informs users about its location, even if it is running in the background.
  • voip - the application provides the ability to make phone calls using an Internet connection.
  • newsstand-content - the application is an aNewsstand application that downloads and processes a file or newspaper in the background.
  • external-accessories - the application works with a hardware accessory, which should deliver updates to the regular schedule through the structure of the external accessory.
  • bluetooth-central - the application works with a Bluetooth accessory, which should deliver updates on a regular schedule through the CoreBluetooth infrastructure.

Note that part of the validation process will verify that your application does what it says is doing in relation to background processing.

+54
Mar 16 2018-12-12T00:
source share

Here is what I did using beginBackgroundTaskWithExpirationHandler .

  • Write a method that starts the background job.
  • Inside this background task, run NSTimer with a scheduled (non-repeating) time of less than 10 minutes. For my situation, I used 5 minutes.
  • When the NSTimer selector starts, complete the background job, and then immediately call the method that you wrote earlier to start another background task.
  • If you want to schedule methods to run at a specific time, you will have to test them in the background task.

This solution is actually not perfect and is still hungry, but will do what you want.

Edit: starting with iOS7, I suggest you read this great post .

+18
Dec 09
source share

In the background, only certain types of applications are allowed to run. See the Implementing Long-Term Background Tasks section of this guide .

If you do not request permission to perform background processing, you can use the UIApplication beginBackgroundTaskWithExpirationHandler , but you cannot get extra time.

+2
Mar 16 2018-12-16T00:
source share

This code makes your iOS application run endlessly in the background. Copy and paste the methods below into a singleton / manager that handles the tasks you need to complete in the background.

 // @interface // Declare Private property @property (nonatomic) UIBackgroundTaskIdentifier backgroundTask; //@end // ... // Copy into //@implementation - (void)setupBackgrounding { [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(appBackgrounding:) name: UIApplicationDidEnterBackgroundNotification object: nil]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(appForegrounding:) name: UIApplicationWillEnterForegroundNotification object: nil]; } - (void)appBackgrounding: (NSNotification *)notification { [self keepAlive]; } - (void) keepAlive { self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask]; self.backgroundTask = UIBackgroundTaskInvalid; [self keepAlive]; }]; } - (void)appForegrounding: (NSNotification *)notification { if (self.backgroundTask != UIBackgroundTaskInvalid) { [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask]; self.backgroundTask = UIBackgroundTaskInvalid; } } 
+2
Nov 15 '13 at 22:36
source share

You can not. If your application does not use audio, voip or gps. What you can do is notify the user (through local notifications) that the time has almost reached and ask him to open / close the application.

Also, if you just need to notify the user, you can use push notifications.

0
Mar 16 2018-12-12T00:
source share

https://github.com/yarodevuci/backgroundTask Check out my code here I use an audio player that plays an empty wav file Works fine on iOS 8 Using about 10% battery over a 24 hour period How to use:

 var backgroundTask = BackgroundTask() backgroundTask.startBackgroundTask() //Starts playing blank audio file. You can run NSTimer() or whatever you need and it will continue executing in the background. backgroundTask.stopBackgroundTask() //Stops the task 

Warning: Apple will refuse this if you try to send it!

0
Aug 31 '16 at 4:40
source share

If your application type is not VOIP / Audio / Location .... (check Background modes ),

or you do not want to specify your application as the background of the application, you can implement beginBackgroundTaskWithName: expirationHandler or beginBackgroundTaskWithExpirationHandler to ask for more time to start your process in the background. Detailed description here

It is expected that applications moving to the background will dive into a dormant state as quickly as possible so that the system can be suspended. If your application is in the middle of a task and it takes a bit of extra time to complete this task, it can call the beginBackgroundTaskWithName: expirationHandler: method or the beginBackgroundTaskWithExpirationHandler: method of the UIApplication object to request additional execution time. Calling any of these methods temporarily suspends the suspension of your application, giving it a little extra time to complete the work. Upon completion of this work, your application must call the endBackgroundTask: method to let the system know that it has completed and may be suspended.

Each call to the beginBackgroundTaskWithName: expirationHandler: or beginBackgroundTaskWithExpirationHandler: method generates a unique token to associate with the corresponding task. When your application completes the task, it must call the endBackgroundTask: method with the appropriate token so that the system knows that the task has completed. Failure to call the endBackgroundTask: method for a background task will terminate your application. If you provided an expiration handler when starting a task, the system calls that handler and gives you the last chance to complete the task and avoid completion.

0
Dec 05 '17 at 9:08 on
source share



All Articles