Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Location Services not working in iOS 7

I've recently upgraded my iOS devices to use iOS 7. One of the apps that we're developing uses background location services to track device location and all of our testers have reported that the app no longer appears to track in the background under iOS 7.

We have verified that backgrounding for the app is enabled in the settings on the device and the previous build worked flawlessly under iOS 6. Even if the device were cycled, the app would restart after a location update.

Is there something else that needs to be done to make this work under iOS 7?

like image 333
Alan Samet Avatar asked Sep 22 '13 17:09

Alan Samet


People also ask

Why can't I turn on Location Services on my iPhone 7?

Go to Settings > Privacy > Location Services > Location Services AND any apps that are enabled. If they are greyed out, check if you have any restrictions on. Go to Settings > General > Restrictions to check.

Why is my Location Services on my iPhone not working?

In the Settings app, tap Privacy, then tap Location Services. Make sure Location Services is on, and make sure Maps is set to While Using the App or Widgets. Set the date, time, and time zone correctly on your device.

How do I enable background locations on my iPhone?

To give an app access to your background location, you have to head to Settings > Privacy > Location Services > [App Name] and select “Always.” Apps have to ask you to do this rather than popping up a prompt requesting access.

How do I fix my location on my iPhone 7?

Just open Settings and tap General -> Reset -> Reset Location & Privacy. You'll need to enter your iPhone passcode to reset your Location Services. This will change your location and privacy settings back to the way they were when you first got your iPhone. Do this, and then try using an app like Maps or Weather again.


2 Answers

Here is the solution that I used to get continuous location from iOS 7 devices no matter it is in foreground or background.

You may find the full solution and explanation from blog and also github:-

  1. Background Location Update Programming for iOS 7 and 8

  2. Github Project: Background Location Update Programming for iOS 7 and 8

Methods and Explanation:-

  1. I restart the location manager every 1 minute in function didUpdateLocations

  2. I allow the location manager to get the locations from the device for 10 seconds before shut it down (to save battery).

Partial Code Below:-

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{  for(int i=0;i<locations.count;i++){     CLLocation * newLocation = [locations objectAtIndex:i];     CLLocationCoordinate2D theLocation = newLocation.coordinate;     CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;     NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];      if (locationAge > 30.0)         continue;      //Select only valid location and also location with good accuracy     if(newLocation!=nil&&theAccuracy>0        &&theAccuracy<2000        &&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){         self.myLastLocation = theLocation;         self.myLastLocationAccuracy= theAccuracy;         NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];         [dict setObject:[NSNumber numberWithFloat:theLocation.latitude] forKey:@"latitude"];         [dict setObject:[NSNumber numberWithFloat:theLocation.longitude] forKey:@"longitude"];         [dict setObject:[NSNumber numberWithFloat:theAccuracy] forKey:@"theAccuracy"];         //Add the vallid location with good accuracy into an array         //Every 1 minute, I will select the best location based on accuracy and send to server         [self.shareModel.myLocationArray addObject:dict];     } }  //If the timer still valid, return it (Will not run the code below) if (self.shareModel.timer)     return;  self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager]; [self.shareModel.bgTask beginNewBackgroundTask];  //Restart the locationMaanger after 1 minute self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self                                                        selector:@selector(restartLocationUpdates)                                                        userInfo:nil                                                         repeats:NO];  //Will only stop the locationManager after 10 seconds, so that we can get some accurate locations //The location manager will only operate for 10 seconds to save battery NSTimer * delay10Seconds; delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self                                                 selector:@selector(stopLocationDelayBy10Seconds)                                                 userInfo:nil                                                  repeats:NO];  } 

Update on May 2014: I got a few requests for adding sample codes on sending the location to server for a certain time interval. I have added the sample codes and also combined a fix for BackgroundTaskManager to solve a glitch for background running over an extended period of time. If you have any questions, you are welcomed to join us for a discussion here: Background Location Update Programming for iOS 7 with Location Update to Server Discussion

Update on January 2015: If you want to get the location update even when the app is suspended, please see: Get Location Updates for iOS App Even when Suspended

like image 175
Ricky Avatar answered Oct 04 '22 16:10

Ricky


If you look at WWDC 2013 Session video #204 - What's new with multitasking pdf, page number 15 clearly mentions that apps wont launch in the background if user kills it from the app switcher. Please see the image,

enter image description here

like image 20
Obj-Swift Avatar answered Oct 04 '22 16:10

Obj-Swift