0

i developed an app which ranges for beacons in the background using ibeacon API. As it uses the core location and bluetooth , so i enabled the Location, Bluetoothconfiguration from the capabilities. So after running my app , ranging happening in background, but after 5-10 min between my app terminates, when i launch the app it is again showing the splash and the login page, so after google i learned that app runs in background with some extra time.

To overcome the app termination i'm using the below code in a method and calling that method in applicationDidEnterBackground.

-(void)startBackgroundTask
 {
if(bgTask != UIBackgroundTaskInvalid)
    {
     [[UIApplication sharedApplication] endBackgroundTask:bgTask];
   bgTask = UIBackgroundTaskInvalid;
      }
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{


   [self endBackgroundUpdateTask];

  NSLog(@"your time is over");
    //you can call start once again to get more time
    }];
  }

 -(void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: bgTask];
bgTask= UIBackgroundTaskInvalid;
[[NSNotificationCenter defaultCenter]
 postNotificationName:@"TimerOutStartMonitering"   object:nil];

}

After using the above code and debugging , my app ranging stops once the UIBackgroundTaskInvalid. How can i achieve the both tasks 1)My App shouldn't terminate once the background time is finished. 2)My ranging for beacons shouldn't stop.

Is it possible? Please help me out.

Madhu
  • 869
  • 1
  • 17
  • 37

1 Answers1

1

Unfortunately, this is not possible. You cannot run a background task like this indefinitely. This mechanism is only intended for short term app cleanup before termination. See here.

Apps running background tasks have a finite amount of time in which to run them. (You can find out how much time is available using the backgroundTimeRemaining property.)

Because of this, iBeacon ranging is limited to a few seconds in the background. You can use IBeacon monitoring to relaunch your app and range again on beacon discovery, but again, you will only get a small of ranging time unless the user brings the app tyo the foreground.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204