1

So, i want my app to do background execution for only a fixed amount of time, this is in case the user does not manually stop the app, and the app therefore in theory could run in background forever(is that even possible?).

I'm using the code below (just a test app) to test how long exactly a background task can run before ending. I read somewhere that 10 minutes is the longest we can do background execution, and there is no way to get beyond that(?). However, my code will only execute in the background for 3 minutes.

So to sum up my questions:

  1. Is it possible to tell the app to execute in the background for x > 10 minutes?

2.Do i have any other options for something similar? (the actual app i need this implemented in, receives location updates in the background, the user could have the phone in the background for as long as 30 minutes, and suddenly not receiving updates would be bad)

 -  (void)viewDidLoad {
    [super viewDidLoad];

    counterTask = [[UIApplication sharedApplication]
               beginBackgroundTaskWithExpirationHandler:^{
                   // do something                   }];
    count=0;
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                                          target:self
                                        selector:@selector(countUp)
                                        userInfo:nil
                                         repeats:YES];
}




- (void)countUp {

    if (count==10000) {
        [theTimer invalidate];

        [[UIApplication sharedApplication] endBackgroundTask:counterTask];
    } else {
        NSLog(@"asd");
        count++;
        NSString *currentCount;
        currentCount=[[NSString alloc] initWithFormat:@"%d",count];
        _theCount.text=currentCount;

        long seconds = lroundf([[UIApplication sharedApplication] backgroundTimeRemaining]);

        NSLog([NSString stringWithFormat:@"%ld",seconds]);

    }
}
Anders
  • 719
  • 8
  • 22

2 Answers2

1

I read somewhere that 10 minutes is the longest we can do background execution, and there is no way to get beyond that(?). However, my code will only execute in the background for 3 minutes.

yes you are right before iOS 7 iOS allowed 10 minutes max for apps to execute in background , however since iOS 7 they have reduced this time to 180 seconds.

But if you want to get Location Updates in background than you can add Required Background modes property in your info.Plist file. Using this you will be able to run your app in background for getting location updates Apple will review your request while reviewing your app for app store submission so be sure to use this mode only if you using it for its actual purpose.

enter image description here

Following are various modes for which apple allows background execution you can take a look at it at Apples Doc on background execution

Back Ground Modes

Edit

If you wish to stop getting location Updates after specific time once user goes to backGround you can do this

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self performSelector:@selector(stopGettingLocationUPdates) withObject:nil   afterDelay:1800];
}

-(void)stopGettingLocationUPdates{
    [self.locationManager stopUpdatingLocation]
}

This will stop updates after 30 mins.

Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
  • Hi Bhumit, and thank you for the response. I have already added the thing to the info.Plist file, exactly as you mention, and my app does work as intended in background, and everything is good.. My concern is just, i want the app to potentially receive updates for max 30 min, or until the user ends it. 3 minutes is not enough. Could the solution be to prevent the app from going to the background?(if thats possible) – Anders Sep 15 '14 at 20:25
  • You can programatically stop recieving location updates once you are done using updates by using stopUpdatingLocation method – Bhumit Mehta Sep 15 '14 at 20:29
  • Preventing the app from going to background is not possible , but if you are using one of the background modes , you can receive location updates as long as you want you wont be restricted to 180 seconds if you are using this background mode. You can stop it once you are done . – Bhumit Mehta Sep 15 '14 at 20:39
  • In some scenario it might happen that iOS aborts your app in order to free some memory if too many apps are running , otherwise your apps keeps running untill user manually closes it. – Bhumit Mehta Sep 15 '14 at 20:42
  • Alright, i tested it, and like you said, i can receive location updates in the background infinite. But what if the user never cancels the updates? The app could keep draining the battery, and the user would not know why... – Anders Sep 16 '14 at 08:37
  • Yes you are right, You can stop getting location updates once you are done getting updates. However unless you programatically call stopUpdatingLocation or user closes app , there is no other way to stop getting updates – Bhumit Mehta Sep 16 '14 at 10:35
  • I will try to implement a static NSTimer and use that to make sure location updates last for no longer than 30 mins. Thank you for your time :) – Anders Sep 16 '14 at 12:39
0

Your code is not running in the background. It is not testing what you want to test.

Apple's docs say:

Executing a Finite-Length Task in the Background Apps that are transitioning to the background can request an extra amount of time to finish any important last-minute tasks. To request background execution time, call the beginBackgroundTaskWithName:expirationHandler: method of the UIApplication class.

The actual time you get is not specified and is probably decided ad hoc based on power consumption, memory needs and so on. They may be a maximum.

They go on to say:

Implementing Long-Running Background Tasks For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that record audio content while in the background.
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that support Voice over Internet Protocol (VoIP)
  • Apps that need to download and process new content regularly
  • Apps that receive regular updates from external accessories Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.
Rog
  • 17,070
  • 9
  • 50
  • 73