SignificantLocationChanges doesn't work since iOS 8

我怕爱的太早我们不能终老 提交于 2019-12-01 21:28:05

In iOS 8 You must request authorization with type "Always" to allow your app to use significant locations.

Add a new row in your -Info.plist file with key NSLocationAlwaysUsageDescription

Then request authorization if its not requested yet.

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if (status == kCLAuthorizationStatusNotDetermined && [manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [manager requestAlwaysAuthorization];
    }
}

I'm having problems with startMonitoringSignificantLocationChanges too..

I added the [self.locationManager requestWhenInUseAuthorization]; and added the NSLocationWhenInUseUsageDescription string to the plist file.

When i run my app everything works, the didChangeAuthorizationStatus delegate method called, but no activity to the didUpdateLocation or didFailWithError delegate methods..

But when i switch to startUpdatingLocation, magically it works! But i need startMonitoringSignificantLocationChanges to work because i dont want my app to consume battery for an event that i dont need!

UPDATE!! Problem Solved!

Oh i see why it does not working now!. The new SDK reference here in this link says;

"You must call this method or the requestAlwaysAuthorization method prior to using location services. If the user grants “when-in-use” authorization to your app, your app can start most (but not all) location services while it is in the foreground. (Apps cannot use any services that automatically relaunch the app, such as region monitoring or the significant location change service.)"

So it is not possible to use startMonitoringSignificantLocationChanges with the [self.locationManager requestWhenInUseAuthorization]; method. You have to use requestAlwaysAuthorization instead!

Have you remembered to call method

-requestAlwaysAuthorization
(or -requestWhenInUseAuthorization)

on your CLLocationManager? This is a new method in iOS 8, and it's required to call it before starting location updates.

Also, double check if you're allocating and calling -startUpdatingLocation on the main thread. I'm not sure about that one, but I think calling it on a different thread could cause problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!