Question

I am trying to make an location app. You can set an alarm for a particular location. When you reach at that location, alarm will start. Now, Problem is that how can i check weather i have reached at my desired location while the app is in background?? According to my knowledge, apple allows any code to run for 10 min max in background. But There are number of apps available that continuously check for particular location in background.

Suggest any way to implement this.

Was it helpful?

Solution 2

i think this article will help you very much

The first thing you need to do is modify your app’s plist. Add the following settings.

Application does not run in background: NO

Required background modes: VOIP

And set up the code for when the application launches

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"Starting app");

    self.powerHandler = [[PowerHandler alloc] init];
    self.lastBatteryState = UIDeviceBatteryStateUnknown;

    UIApplication* app = [UIApplication sharedApplication];

    self.expirationHandler = ^{
        [app endBackgroundTask:self.bgTask];
        self.bgTask = UIBackgroundTaskInvalid;
        self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
        NSLog(@"Expired");
        self.jobExpired = YES;
        while(self.jobExpired) {
            // spin while we wait for the task to actually end.
            [NSThread sleepForTimeInterval:1];
        }
        // Restart the background task so we can run forever.
        [self startBackgroundTask];
    };
    self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];

    // Assume that we're in background at first since we get no notification from device that we're in background when
    // app launches immediately into background (i.e. when powering on the device or when the app is killed and restarted)
    [self monitorBatteryStateInBackground];

    return YES;
}

Next, we set up the background job

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"Entered background");
    [self monitorBatteryStateInBackground];
}

- (void)monitorBatteryStateInBackground
{
    NSLog(@"Monitoring battery state");
    self.background = YES;
    self.lastBatteryState = UIDeviceBatteryStateUnknown;
    [self startBackgroundTask];
}

- (void)startBackgroundTask
{
    NSLog(@"Restarting task");
    // Start the long-running task.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // When the job expires it still keeps running since we never exited it. Thus have the expiration handler
        // set a flag that the job expired and use that to exit the while loop and end the task.
        while(self.background && !self.jobExpired)
        {
            [self updateBatteryState:[self.powerHandler checkBatteryState]];
            [NSThread sleepForTimeInterval:1];
        }

        self.jobExpired = NO;
    });
}



- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */

    NSLog(@"App is active");
    self.background = NO;
}

OTHER TIPS

You can use the region monitoring capability of the core-location framework

You can define your destination as a region and then you will get a call to your delegate's locationManager:didEnterRegion: method, even if you are in the background.

As you are not actively executing, the 10 minute background task limit does not apply.

i would use regionmonitoring then you get called in the delegatemethods

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

and

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region

There are two ways.

  1. First to enable location in background, add key UIBackgroundModes with value location in info.plist file. It will now allow your application to fetch the location even after 10 mins from when the application goes into background. So on each new location compare its distance with desired location. If it is with in permissible limit, do the action.
  2. For desired area, you should create a region and you should start observing those regions using location manager's delegates. This will allow your app to launch itself even if it is not in running mode. iOS will launch your app automatically if it is not running.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top