SignificantLocationChanges doesn't work since iOS 8

自闭症网瘾萝莉.ら 提交于 2019-12-20 01:43:54

问题


I have a problem with the SignificantLocationChanges since the release of iOS 8. The method

[locationManager startMonitoringSignificantLocationChanges]; 

is called correctly after checking for availability, the delegates also work nice (I check it with the didChangeAuthorizationStatus method, which is part of the same delegate and object) and compiler has no doubts, but there comes absolutely no updates and no errors from the didFailWithError method. The log says the authorizationStatus is 4, which is ok I think.

Before iOS 8 this all works fine.

The first test-device (iPad 2 with 3G) runs iOS 7.1.2 the second (iPhone 5) 8.0.2, when I use the normal startUpdatingLocation method I get updates immediately. But SignificantLocationChanges would be much better for my work. Has anyone an idea where the error could be?


回答1:


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];
    }
}



回答2:


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!




回答3:


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.



来源:https://stackoverflow.com/questions/26073005/significantlocationchanges-doesnt-work-since-ios-8

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