从iOS 8开始,重要位置变化无效

时间:2014-09-27 08:54:36

标签: ios objective-c iphone core-location

自iOS 8发布以来,SignificantLocationChanges出现了问题。方法

[locationManager startMonitoringSignificantLocationChanges]; 
检查可用性后,

被正确调用,代理也工作得很好(我用didChangeAuthorizationStatus方法检查它,这是同一个委托和对象的一部分)编译器毫无疑问,但绝对没有更新,didFailWithError方法没有错误。日志说 authorizationStatus 是4,我认为没问题。

在iOS 8之前,一切正常。

第一个测试设备(带有3G的iPad 2)运行iOS 7.1.2第二个(iPhone 5)8.0.2,当我使用正常startUpdatingLocation方法时,我立即获得更新。但是SignificantLocationChanges对我的工作会好得多。有谁知道错误可能在哪里?

3 个答案:

答案 0 :(得分:5)

在iOS 8中您必须请求“始终”类型的授权才能允许您的应用使用重要位置。

使用密钥NSLocationAlwaysUsageDescription在-Info.plist文件中添加新行 enter image description here

如果尚未请求授权,则请求授权。

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

答案 1 :(得分:3)

我也遇到了startMonitoringSignificantLocationChanges的问题..

我添加了[self.locationManager requestWhenInUseAuthorization];并将NSLocationWhenInUseUsageDescription字符串添加到plist文件中。

当我运行我的应用程序时,一切正常,didChangeAuthorizationStatus委托方法被调用,但didUpdateLocationdidFailWithError委托方法没有活动..

但是当我切换到startUpdatingLocation时,神奇地它会起作用!但是我需要startMonitoringSignificantLocationChanges才能工作,因为我不希望我的应用程序为我不需要的事件消耗电池!

<强> UPDATE !!问题解决了!

哦,我明白为什么它现在不起作用!新的SDK参考here in this link说;

&#34;您必须在使用位置服务之前调用此方法或requestAlwaysAuthorization方法。如果用户向您的应用授予“使用中”授权,您的应用可以启动大多数(但不是全部)位置服务,同时它位于前台。 (应用不能使用任何自动重新启动应用的服务,例如区域监控或重要的位置变更服务。)&#34;

因此无法将startMonitoringSignificantLocationChanges[self.locationManager requestWhenInUseAuthorization];方法一起使用。您必须改为使用requestAlwaysAuthorization

答案 2 :(得分:0)

你有没有记得叫方法

-requestAlwaysAuthorization
(or -requestWhenInUseAuthorization)
你的CLLocationManager上有

吗?这是iOS 8中的一种新方法,在开始位置更新之前需要调用它。

另外,请仔细检查您是否在主线程上分配并调用-startUpdatingLocation。我不确定那个,但我认为在不同的线程上调用它可能会导致问题。