6

For the bounty, I am not interested in GPS or audio background modes as the former uses too much of the battery and the latter prevents any other audio from being used, otherwise facing audio interruption, thus ending background processes. I will need a way to be continuously processing in the background, so background modes that trigger occasionally are also out of the question.

If there is some way to run the application in the background, even by ignoring Apple's rules, I am interested in trying it. If the answer is VOIP, I am unsure where to begin the implementation, as all of my research has come up too high level or as a failure. How will my application be able to run in the background using the VOIP background mode. Without any added code, the application refuses to run in the background.


I know that with iOS 7, background modes have changed again. I would like to be able to run my application (that will never need to be approved on the iOS App Store) in the background. I would also like to be able to stop execution in the background until a specific time in the future.

For example, I would like it to run a process for 15 minutes, schedule the next task and then sleep until that time. For now, I've had to run a silent track in the background for background processing, but I would like to be able to have the application truly sleep during that time - also, playing real music or making a phone call are "handy features" of the iPhone and I don't like losing them.

I know there is also GPS, but that consumes an enormous amount of battery. The other background modes don't seem to give full control of background processing and timing to the application and leave a large portion of the timing and execution duration to the OS.

What I need is to be able to have my application process in the background for minutes at a time and then sleep until a fairly specific interval and continue processing. Is this possible with a better approach than I am currently using?

I've seen that VOIP used to be a possibility, but I'm not sure that it will work, as I don't need the application to run one simple task in the background, but rather to continue whatever was running in the foreground before the application was pushed to the background. Also, individual tasks could take upwards of 1 hour to complete, so they won't be able to transfer when the background task expires. All of my assumptions are based off this thread.

3
  • Here's a hacky way: you could "play music" in the background so that the app stays "awake".. Feb 8, 2014 at 6:25
  • @Jugale As stated in my question, that's exactly what I'm currently doing. Thank you for brainstorming, though!
    – RileyE
    Feb 8, 2014 at 6:26
  • Silly me.. I guess my brain went missing for a moment there Feb 8, 2014 at 6:29

5 Answers 5

2

Edit: There seems to be a terrible drop off rate with this method. At random, the recursion will seemingly fail for seemingly no reason (maybe a system timeout on execution?). If I place the recursion before ending the background task, the OS kills my application, but if I put it after, it occasionally seems to stop the background tasks at some point. I have seen it stop in the middle of my "allotted background time", as well.

In short, the below method does seem to run indefinitely, but not infinitely. Is there either a way to make the runtime guaranteed to be infinite or another solution?


It seems that using VOIP was leagues easier than I had first thought.

All that is required to run my application indefinitely (unfortunately sleeping is not an option) is to add voip to the selected Background Modes, either in the plist or in Target's Capabilities. After that, adding and running this code once, in an object that is never deallocated (your AppDelegate works nicely here), will allow for infinite background processing time:

- (void)infiniteBackgroundLoop
{
    __block UIApplication *applicationBlockReference = [UIApplication sharedApplication];
    __block AppDelegate *appDelegateBlockReference = self;
    __block UIBackgroundTaskIdentifier backgroundTask = [applicationBlockReference beginBackgroundTaskWithExpirationHandler:^
                                                         {
                                                             [applicationBlockReference endBackgroundTask:backgroundTask];
                                                             backgroundTask = UIBackgroundTaskInvalid;
                                                             [appDelegateBlockReference infiniteBackgroundLoop];
                                                         }];
}

In order to allow sleeping indefinitely, add a break to the recursion.

-1

I used background fetch to achieve something similar. You can use this to keep your app active in the background.

Xcode target capabilities

1
  • 2
    That doesn't allow the application to run indefinitely, though.
    – RileyE
    Feb 18, 2014 at 18:56
-1

I have the a demo, see if it helps you:

Add these properties to your .h file -

@property (nonatomic, strong) NSTimer *updateTimer;
@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;

Now suppose you have a action on button --> btnStartClicked then your method would be like :

-(IBAction)btnStartClicked:(UIButton *)sender {
    self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                        target:self
                                                      selector:@selector(calculateNextNumber)
                                                      userInfo:nil
                                                       repeats:YES];
    self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"Background handler called. Not running background tasks anymore.");
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }];

}

 -(void)calculateNextNumber{
    @autoreleasepool {
      // this will be executed no matter app is in foreground or background
    }
}

and if you need to stop it use this method,

- (IBAction)btnStopClicked:(UIButton *)sender {

    [self.updateTimer invalidate];
    self.updateTimer = nil;
    if (self.backgroundTask != UIBackgroundTaskInvalid)
    {
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }
    i = 0;
}
1
  • If you can see my answer, I already have this, but on a loop. This would only run for 180 seconds at most.
    – RileyE
    Feb 19, 2014 at 22:05
-1

We also played with background modes in our app, and I check all solution that found, and can say that there is only one way to stay active in background and is not "VOIP", because "VOIP" gives your app wake-up every 5-6 minutes not infinity run.

In documentation about setKeepAliveTimeout:handler: you can see that this method will call handler block at minimum every 600 second, and block has a maximum of 10 seconds to perform any needed tasks and exit.

To clean this you can add NSLog(@"time remain: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]); to your infiniteBackgroundLoop implementation. Because second and next call beginBackgroundTaskWithExpirationHandler not get same time of background run as first call.

Another working way is Location Manager, yes is bad because use lot of battery but is get you that you want - app newer stop.

The implementation can be easily found there

5
  • VOIP has worked for running in the background so far. However, what do you mean by "check all solution that found"?
    – RileyE
    Feb 18, 2014 at 18:58
  • I want say that I tried all variants: sound, VoIP and location
    – sage444
    Feb 18, 2014 at 20:06
  • So, you're saying that VoIP isn't the correct way, but what is?
    – RileyE
    Feb 18, 2014 at 20:57
  • This won't allow the application to run continuously in the background, either. And GPS uses far too much battery.
    – RileyE
    Feb 19, 2014 at 22:06
  • It cannot be applied to iOS 7 and later. Background time is restricted to 180 sec.
    – Avinash
    Oct 27, 2014 at 9:01
-1

You could use background fetch and set the regresh rate to a short NSTimeInterval.

In your didFinishLaunchingWithOptions: try to add:

[[UIApplication sharedApplication] setMinimumBackgroundFetch:1];

I haven't tested this, let me know if this could be a starting point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.