Running an iOS app in the background for more than 10 minutes

I am trying to allow my application to run in the background for more than 10 minutes, in accordance with this and my good below. (I want to use a long background to track location, my code here just uses a counter for testing purposes) Can anyone point out what the problem is?

Realization of long-term background tasks

For tasks that require longer execution time, 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 to the user in the background, such as a music player application

Applications that constantly inform users of their location, such as a navigation application

Voice over Internet Protocol (VoIP) Applications

Press apps that need to download and process new content. Applications that receive regular updates from external accessories

Applications implementing these services should declare the services they support and use the system framework to implement the relevant aspects of these services. The announcement of the services allows the system to know which ones you are using, but in some cases the system ones actually prohibit the suspension of your application.

   - (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 launch an iOS application in the background?

---- , 10 , , ?

- (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]; } }
+5
4

, . .

Info.plist:

  • , location-services UIRequiredDeviceCapabilities
  • GPS, gps UIRequiredDeviceCapabilities
  • , 10 , location UIBackgroundModes. 10- .
  • NSLocationUsageDescription ( )

, , . , . , , .

. , , , , . , - , .

+4

. phix23 answer ( ) , , .

, .

. , beginBackgroundTaskWithExpirationHandler:. , , , , .

, , CLLocationManager. , . , - - . , , countUp , , .

+1

applicationDidEnterBackground, , :

[app beginBackgroundTaskWithExpirationHandler:^{}];

Then you invalidate the long task at willEnterForeground.

I recently succeeded in iOS 6, but I'm not sure if it will be approved for the store.

+1
source

By the way, in your plist you can install alos Required background modes, and then in Item0App registers for location updates

0
source

All Articles