自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
对我的工作会好得多。有谁知道错误可能在哪里?
答案 0 :(得分:5)
在iOS 8中您必须请求“始终”类型的授权才能允许您的应用使用重要位置。
使用密钥NSLocationAlwaysUsageDescription在-Info.plist文件中添加新行
如果尚未请求授权,则请求授权。
- (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
委托方法被调用,但didUpdateLocation
或didFailWithError
委托方法没有活动..
但是当我切换到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
。我不确定那个,但我认为在不同的线程上调用它可能会导致问题。