自iOS 8以来,SignificantLocationChanges不起作用
自iOS 8发布以来,我有一个与SignificantLocationChanges
的问题
[locationManager startMonitoringSignificantLocationChanges];
在检查可用性之后调用是否正确,委托也工作得很好(我使用didChangeAuthorizationStatus
方法检查它,它是同一个委托和对象的一部分),编译器毫不怀疑,但绝对没有更新,没有错误didFailWithError
方法。 日志说authorizationStatus是4,这是我认为没关系。
在iOS 8之前,这一切工作正常。
第一个testing设备(带有3G的iPad 2)运行iOS 7.1.2第二个(iPhone 5)8.0.2,当我使用正常的startUpdatingLocation
方法时,我立即得到更新。 但是SignificantLocationChanges
对我的工作会更好。 有没有人有一个想法,错误可能是?
在iOS 8中,您必须请求“Always”types的授权,以允许您的应用使用重要位置。
在密钥NSLocationAlwaysUsageDescription的-Info.plist文件中添加一个新行
然后请求授权,如果它还没有请求。
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { if (status == kCLAuthorizationStatusNotDetermined && [manager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [manager requestAlwaysAuthorization]; } }
我在startMonitoringSignificantLocationChanges
也有问题..
我添加了[self.locationManager requestWhenInUseAuthorization];
并将NSLocationWhenInUseUsageDescription
string添加到plist文件中。
当我运行我的应用程序一切正常, didChangeAuthorizationStatus
委托方法调用,但didUpdateLocation
或didFailWithError
委托方法没有活动..
但是,当我切换到startUpdatingLocation
,神奇的是它的作品! 但我需要startMonitoringSignificantLocationChanges
工作,因为我不希望我的应用程序消耗电池的事件,我不需要!
UPDATE! 问题解决了!
哦,我明白为什么它现在不工作! 在这个链接新的SDK参考在这里说;
“在使用位置服务之前,您必须调用此方法或requestAlwaysAuthorization方法。如果用户授予您的应用程序”使用时“授权,您的应用程序可以在前台启动大多数(但不是全部)位置服务(应用程序不能使用自动重新启动应用程序的任何服务,例如区域监控或重要位置更改服务)。“
所以不能使用startMonitoringSignificantLocationChanges
和[self.locationManager requestWhenInUseAuthorization];
方法。 你必须改用requestAlwaysAuthorization
!
你记得打电话给方法吗?
-requestAlwaysAuthorization (or -requestWhenInUseAuthorization)
在你的CLLocationManager? 这是iOS 8中的一种新方法,需要在启动位置更新之前调用它。
此外,请仔细检查您是否正在主线程上分配和调用-startUpdatingLocation
。 我不确定这一点,但我认为在不同的线程上调用可能会导致问题。