【问题标题】:What's the proper way to use startMonitoringSignificantLocationChanges in swift 4?在 swift 4 中使用 startMonitoringSignificantLocationChanges 的正确方法是什么?
【发布时间】:2017-12-16 23:47:00
【问题描述】:

我尝试制作一个正在运行的应用程序,我想使用 startMonitoringSignificantLocationChanges 方法,但我遗漏了一些东西,因为没有像文档所说的那样触发 didUpdateLocations

我已成功使用 CLLocationManager 类的 startUpdatingLocation 方法,我完全可以将它用于我的兴趣,但我想了解为什么我的实现不起作用。

所以,我有这个代码:

let locationManager = CLLocationManager()
func determineMyCurrentLocation() {

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.allowsBackgroundLocationUpdates = true

    print("authorization Status: \(CLLocationManager.authorizationStatus().rawValue)")

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation() //(1)
        //(2)locationManager.startMonitoringSignificantLocationChanges()
        locationManager.distanceFilter = 20
        //locationManager.start
        //locationManager.startUpdatingHeading)(
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("yaaay!") }

如果您评论 (1) 并取消评论 2,则 func locationManager...etc 永远不会被触发。

另外,我的 info.plist 设置了正确的键,“隐私 - 使用时的位置使用说明”和“隐私 - 始终位置和使用时的位置使用说明”)。

有什么想法吗?

提前致谢。

已编辑:我的问题不准确。我将 locationManager 属性作为类成员,并在 info.plist 中完成了属性

【问题讨论】:

  • 您的位置管理器需要是一个属性,而不是函数退出时将释放的局部变量。此外,您还需要 iOS 11 的新权限密钥 - “alwaysandwheninuse”

标签: ios core-location swift4


【解决方案1】:

startMonitoringSignificantLocationChanges 始终需要授权。这也意味着您应该在功能部分打开“后台模式”并选择“位置更新”。您还需要Privacy - Location Always Usage Description 尝试这两个,您应该会收到重要的位置更改更新。

【讨论】:

  • 这不是真的。使用startMonitoringSignificantLocationChanges 的优点之一是您不必在 plist 中设置背景模式 - 位置(Apple 会仔细检查它以确保它对您的应用程序真的很重要 - 毕竟这是一个严重的隐私问题,和电池耗尽)。在开始监视位置更改之前,您必须通过通常的requestAlwaysAuthorization 获得用户的跟踪权限。并且不要设置CLLocationManage.allowsBackgroundLocationUpdates,因为这将导致没有背景模式 - 位置设置的断言。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
相关资源
最近更新 更多