0

I want to make my Application Active when Application is in background state, as my NStimer and other operation are performing. So, I did the following code to make my Application Active.

 - (void)applicationWillResignActive:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];
     if (backgroundTaskBool==TRUE) 
      {


      bgTask = [app beginBackgroundTaskWithExpirationHandler:^{

    dispatch_async(dispatch_get_main_queue(), ^{

        if (bgTask != UIBackgroundTaskInvalid)
        {
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }
    });
}];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the work associated with the task.

    _locationManager.distanceFilter = 100;
    _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [_locationManager startMonitoringSignificantLocationChanges];
    [_locationManager startUpdatingLocation];
     dispatch_async(dispatch_get_main_queue(), ^{
        if (bgTask != UIBackgroundTaskInvalid)
        {
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }
      });
   });
  }
 }

I just wanted to know is it a correct way to do? or there will be some other way to achieve this, because this process is taking too much battery and unnecessary location updating. So please help me out from this.

Thanks in advance.

Raptor
  • 53,206
  • 45
  • 230
  • 366
Bhrigesh
  • 861
  • 1
  • 7
  • 14

2 Answers2

0

You cant run the task in back ground for longer. IOS allow you to run the background task for 10 min only (after you click home button once your app launched).

Refer this https://stackoverflow.com/a/5326257/1545180

Community
  • 1
  • 1
Ganapathy
  • 4,594
  • 5
  • 23
  • 41
0

As your question is tagged for iOS6! You application cannot continue to run in background unless your application requires any of these features.

  • Audio—The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)
  • Location—The app keeps users informed of their location, even while it is running in the background.
  • VoIP—The app provides the ability for the user to make phone calls using an Internet connection.
  • Newsstand-content—The app is a Newsstand app that downloads and processes magazine or newspaper content in the background.
thatzprem
  • 4,697
  • 1
  • 33
  • 41