2014-09-27 52 views
3

我与SignificantLocationChanges一个问题,因为iOS的8的方法SignificantLocationChanges不起作用,因为iOS的8

[locationManager startMonitoringSignificantLocationChanges]; 

被称为正确检查可用性后,与会代表还不错的工作的释放(我检查它与didChangeAuthorizationStatus方法是同一个委托和对象的一部分),编译器毫无疑问,但绝对没有更新,也没有来自didFailWithError方法的错误。日志说authorizationStatus是4,这是我认为没关系。

在iOS 8之前,这一切正常。

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

+0

或许表现出一定的代码。就像我说这是工作的罚款,我 – Paulw11 2014-09-27 10:21:14

回答

5

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

在-Info.plist文件中添加一个新行键NSLocationAlwaysUsageDescription enter image description here

然后请求授权如果尚未要求。

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

你有没有想起调用方法

-requestAlwaysAuthorization 
(or -requestWhenInUseAuthorization) 
您CLLocationManager

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

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

3

我在与startMonitoringSignificantLocationChanges太多的问题..

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

当我运行我的应用程序一切正常,该didChangeAuthorizationStatus委托方法调用,但是到了didUpdateLocationdidFailWithError委托方法没有任何活动..

但是当我切换到startUpdatingLocation,奇迹般地它的作品!但我需要startMonitoringSignificantLocationChanges工作,因为我不希望我的应用程序消耗电池的事件,我不需要!

更新!问题解决了!

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

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

所以这是不可能使用startMonitoringSignificantLocationChanges[self.locationManager requestWhenInUseAuthorization];方法。你必须使用requestAlwaysAuthorization而不是!

+0

:) – msmialko 2014-09-28 09:45:42

+0

没有,你必须使用它们两个。在iOS 10上测试。 – AndaluZ 2017-07-13 10:18:04