0

How to remain getting the location continuously in the background in iOS8 and you must try your best to save electricity? Can anybody give me some advice?

2 Answers2

1

In your project setting, Select Target and go to Capabilities, turn ON the background mode and tick the location updates and background fetch. This will add background mode in your project plist.

Now, to get continuous location updates even in background, add this code in AppDelegate's applicationDidEnterBackground: method. This code will kill the background task every time and restart it. So, even when app is in background, you'll receive background location updates.

- (void)applicationDidEnterBackground:(UIApplication *)application {
    if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
        if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
            UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance

            __block UIBackgroundTaskIdentifier background_task; //Create a task object

            background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
                [application endBackgroundTask:background_task]; //Tell the system that we are done with the tasks
                background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
                //System will be shutting down the app at any point in time now
            }];
        }
    }
}

Now to extend device battery life, you can use locationManager:didUpdateLocations: method as it is only called when location changes according to desired accuracy.

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];
    if (location != nil) {
        strLatitude  = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
        strLongitude = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
    }
}
Tejvansh
  • 676
  • 4
  • 9
  • In this way, Can I only change the desired accuracy to save the electricity? But I think it's still use much electricity. – Mitchell-Dream-God May 13 '15 at 09:00
  • Dear Tejvansh, i follow this code its working fine but location updates stop after 3 to 4 minutes.Could you please tell why location updates stop.Thanks ! – vky Dec 20 '17 at 07:32
0

Found some Solution here

Study the example from Ray Wenderlich. The sample code works perfectly.

You can add timer to it by using this code snippet this may reduce battery consumption a bit :

-(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];
}
Community
  • 1
  • 1
Adeel Ahmed
  • 161
  • 1
  • 8
  • Now, I use this way to solve the problem, but it also use much electricity. So I have a idea as follow: `- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *location = [locations lastObject]; [save locationMessage] [self.locationManager stopUpdatingLocation] } ` But I find a problem is that when I stop the location, it will never be started . – Mitchell-Dream-God May 13 '15 at 08:43