【问题标题】:SignificantLocationChanges doesn't work since iOS 8自 iOS 8 起,重要位置更改不起作用
【发布时间】:2014-09-27 08:54:36
【问题描述】:

自iOS 8发布以来SignificantLocationChanges有问题。方法

[locationManager startMonitoringSignificantLocationChanges]; 

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

在 iOS 8 之前,这一切都可以正常工作。

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

【问题讨论】:

  • 也许显示一些代码。对我来说效果很好

标签: ios objective-c iphone core-location


【解决方案1】:

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

在您的 -Info.plist 文件中添加一个带有键 NSLocationAlwaysUsageDescription 的新行

如果还没有请求,则请求授权。

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

【讨论】:

    【解决方案2】:

    我也遇到了startMonitoringSignificantLocationChanges 的问题..

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

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

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

    更新!!问题解决了!

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

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

    因此不能将startMonitoringSignificantLocationChanges[self.locationManager requestWhenInUseAuthorization]; 方法一起使用。你必须改用requestAlwaysAuthorization

    【讨论】:

    • 不,你必须同时使用它们。在 iOS 10 上测试。
    【解决方案3】:

    你记得调用方法吗

    -requestAlwaysAuthorization
    (or -requestWhenInUseAuthorization)
    

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

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

    【讨论】:

      猜你喜欢
      • 2015-02-23
      • 2014-09-12
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 2015-12-11
      相关资源
      最近更新 更多