Muruganandham K
Muruganandham K

Reputation: 5331

Automatic ON/OFF the GPS tracking in an app running in Background

How can i stop and start the GPS tracking when an application running in background. i Can able to stop tracking in the application, but didn't got any way to start the application. I looking for answers for followings.

1.whether i can use push notification silently to start GPS tracking. 2.I tried with local notification, but it require user interaction to start the process. Is there any best ways to do this.

My problem: Initially i can able start tracking the user location and can stop that after some time in background. I wanted to start the tracking next day. Im looking for a way to start tracking in background.

Actually my application is basically the location tracking application. if the launches the application, the app starts tracking until the time 8:00 pm (stops automatically). Again started tracking next day 8:00 am.

Upvotes: 1

Views: 589

Answers (2)

Alex Pavlov
Alex Pavlov

Reputation: 1001

If you started location updates while your app is running in background, these updates will not last longer than whatever time iOS granted for your background task (currently 180 sec).

Location updates must be started while the app is active, doing this ensures the updates keep coming even after your app goes to background, assuming you configured your app background modes properly - see Capabilities/Background Modes/Location updates in Xcode.

Even though, if your app is terminated, the delivery of new location events stops altogether.

If you want to temporary suppress standard location updates, please use

allowDeferredLocationUpdatesUntilTraveled:timeout:

When updates are deferred, GPS receiver is still powered, but no CPU activity happens, as the receiver will not trigger events. I did not test power consumption in this mode myself, but people mention ~2% battery drain per hour.

Upvotes: 1

NSSwift
NSSwift

Reputation: 215

Have you tried this

-(void)applicationDidEnterBackground {
  [self.locationManager stopUpdatingLocation];

  UIApplication* app = [UIApplication sharedApplication];

  bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
  }];

  self.timer = [NSTimer scheduledTimerWithTimeInterval:intervalBackgroundUpdate
                                                target:self.locationManager
                                              selector:@selector(startUpdatingLocation)
                                              userInfo:nil
                                               repeats:YES];
}

Set intervalBackground Update to 1 day

Upvotes: 0

Related Questions