Running iOS App in background for more than 10 minutes

14,611

Solution 1

So your app uses location services. Then please read the Location Awareness Programming Guide.

You need to make some changes to your Info.plist:

  • If your app relies on location services to function properly, add location-services to UIRequiredDeviceCapabilities
  • if your app requires GPS hardware, add gps to UIRequiredDeviceCapabilities
  • if you need to run your app longer then 10 minutes in the background, add location to UIBackgroundModes. Then your location manager will deliver locations beyond the 10-minute-limit.
  • you should also set NSLocationUsageDescription (can also be localized)

Getting Location Events in the Background

If your app needs location updates delivered whether the app is in the foreground or background, there are multiple options for doing so. The preferred option is to use the significant location change service to wake your app at appropriate times to handle new events. However, if your app needs to use the standard location service, you can declare your app as needing background location services.

An app should request background location services only if the absence of those services would impair its ability to operate. In addition, any app that requests background location services should use those services to provide a tangible benefit to the user. For example, a turn-by-turn navigation app would be a likely candidate for background location services because of its need to track the user’s position and report when it is time to make the next turn.

Solution 2

See phix23's answer (and the documentation) for the details, but here I want to explain what you can expect to happen.

This is all pretty much covered in the documentation that you quoted from.

Any application can run in the background for up to ten minutes. That's what the beginBackgroundTaskWithExpirationHandler: method does. No matter which flags and options you set, that's all you'll get using that method.

For apps that need to keep track of location you can use CLLocationManager. This does not allow your app to run in the background as long as you like. Instead it notifies you when something interesting happens -- that's what the delegate is for. So you can't guarantee that your countUp method gets called every ten minutes, but you can have the OS call it when the user moves their handset a certain distance.

Solution 3

By adding the following in the applicationDidEnterBackground method, it seems that it can execute forever:

[app beginBackgroundTaskWithExpirationHandler:^{}];

Then you invalidate the long task in the willEnterForeground.

I succeed in iOS 6 recently, but I'm not sure it would be approved for the store.

Share:
14,611
Ahmad
Author by

Ahmad

Updated on September 21, 2022

Comments

  • Ahmad
    Ahmad over 1 year

    I am trying to allow my app to run in the background for more that 10 minutes, according to this and my good below. (I want to use long background running to keep track of a location, my code here simply just use a counter for testing purposes) Anyone can help point out what the problem is?

    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 keep users informed of their location at all times, such as a navigation app

    Apps that support Voice over Internet Protocol (VoIP)

    Newsstand apps that need to download and process new content 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.

       - (void)viewDidLoad {
                 [super viewDidLoad];
    
            counterTask = [[UIApplication sharedApplication]
                           beginBackgroundTaskWithExpirationHandler:^{
    
                           }];
                    count=0;
    
            theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                                                      target:self
                                                    selector:@selector(countUp)
                                                    userInfo:nil
                                                     repeats:YES];
             }
        - (void)countUp {
    
            {
                count++;
                NSString *currentCount;
                currentCount=[[NSString alloc] initWithFormat:@"%ld",count];
                theCount.text=currentCount;
                [currentCount release];
            }
            }
    

    Another Question: Can I have an iOS App run in the background forever?

    ----Edited code to add location, still doesnt run for more than 10 mins, any help to what i'm doing wrong?

    - (void)viewDidLoad {
         [super viewDidLoad];
    
     count=0;
    
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
     [locationManager startUpdatingLocation];
    
    counterTask = [[UIApplication sharedApplication]
                   beginBackgroundTaskWithExpirationHandler:^{
                       // If you're worried about exceeding 10 minutes, handle it here
    
                       [locationManager startUpdatingLocation];
                   }];
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                                              target:self
                                            selector:@selector(countUp)
                                            userInfo:nil
                                             repeats:YES];
    
     }
    
    
    
         (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
     NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude); NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
    
        count++; NSString *currentCount;
     currentCount=[[NSString alloc] initWithFormat:@"%ld",count];
     theCount.text=currentCount; [currentCount release];
     }
    
        (void)countUp { [locationManager startUpdatingLocation];
     { count++; NSString *currentCount; 
    currentCount=[[NSString alloc] initWithFormat:@"%ld",count]; 
    theCount.text=currentCount;
     [currentCount release]; } }