12

I have a very simple app example that initialises and updates the users location.. On device it successfully throws another callback location every second or so however on device (iPhone running iOS7) is calls the method once and then mysteriously stops...

//Setup Location Manager in ViewDidLoad
locationManager = [[CLLocationManager alloc] init];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDelegate:self];
[locationManager startUpdatingLocation];

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
     NSLog(@"location services not turned on");
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
     NSLog(@"loactions %@", locations);
}

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{

    NSLog(@"new location %f, and old %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}

In iOS6 this app worked perfectly and continuously updated the devices location... What has changed since iOS7?

Khaled Barazi
  • 8,681
  • 6
  • 42
  • 62
Taylor Abernethy Newman
  • 1,162
  • 1
  • 11
  • 22
  • didUpdateLocations method is triggering or not? – karthika Oct 16 '13 at 05:54
  • its triggering but only once... – Taylor Abernethy Newman Oct 16 '13 at 06:03
  • Are you call stopUpdatingLocation in this method? – karthika Oct 16 '13 at 06:05
  • Yes, I seemed to solve the problem by changing the DesiredAccuracy to BestForNavigation... – Taylor Abernethy Newman Oct 16 '13 at 09:06
  • Probably it won't be the solution but the delegate method locationManager:didUpdateToLocation:fromLocation: is deprecated since ios6, if you aren't deploy on lower than ios6 devices it is not needed. – Andrea Jan 13 '14 at 09:45
  • See `disallowDeferredLocationUpdates` from [CLLocationManager Class Reference](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html). Just because you did not set it does not mean Apple did not change it (remember when the flipped the mouse scroll wheel direction without asking?). – jww Jan 13 '14 at 13:05
  • Hey Spectra, I ended up fixing the problem from a few points but what you have put is a great solution so I will mark it correct. T – Taylor Abernethy Newman Jan 23 '14 at 00:13

1 Answers1

12

There are a few things here:

1- I don't see anywhere the property: pausesLocationUpdatesAutomatically. The default for this property is Yes. This means that depending on your activityType (see #2) below, the GPS will pause updates and this could be the reason you are not getting updates. The algorithm is a black box that only Apple knows and maybe it somehow changed between iOS6 and iOS7. Setting pausesLocationUpdatesAutomatically to NO can impact the battery.

2- You should set your activityType depending on your application. The default is CLActivityTypeOther which I am not sure how it impact the GPS algorithm and #1 above. So in order to test your app initially, I would set the activityType appropriately and change pausesLocationUpdatesAutomatically to No. In my test, I would get an update about every second consistently (I tested it overnight).

3- Location updates testing requires movement. In order to get better results, I would use the activityType you set, for testing. In other words, if activityType is CLActivityTypeFitness, I would walk around to test it, etc..

4- locationManager didUpdateToLocation fromLocation is deprecated in iOS7. In addition if locationManager didUpdateLocations is implemented, the former will not be called. So in your case above, locationManager didUpdateToLocation fromLocation is not being called.

5- There is no real battery usage difference between kCLLocationAccuracyBestForNavigation and kCLLocationAccuracyBest. On the other hand, kCLLocationAccuracyBestForNavigation uses top speed GPS and in addition combines it with accelerometer data.

So I would start with setting activityType, setting pausesLocationUpdatesAutomatically to NO and changing desiredAccuracy to kCLLocationAccuracyBestForNavigation. Once you are getting continuous updates, then I would set pausesLocationUpdatesAutomatically to Yes and try to work the code to achieve the same app usability.

Hope this helps

Khaled Barazi
  • 8,681
  • 6
  • 42
  • 62
  • What if I put my app in background, walk to POINT A, then stop moving for 20 min. Then I start moving again to POINT B. During all this time, the app is still in background. When I open my app back in foreground mode, will didUpdateLocations give me all the locations between POINT A and POINT B? – Van Du Tran May 13 '14 at 20:25
  • No it will not, the location that is received by the GPS chip will be passed on via the method locationManager:didUpdateLocations directly. So if you want to store a path of locations you'll have to store them directly when you receive them. – Leon Jan 23 '15 at 10:02