【问题标题】:startMonitoringSignificantLocationChanges not working in swiftstartMonitoringSignificantLocationChanges 不能快速工作
【发布时间】:2016-03-21 01:41:17
【问题描述】:

我在 AppDelegate 文件中使用 Swift 在我的应用中添加了 CLLocationManager。

在 Appdelegate.swift 文件中,

import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var locationManager: CLLocationManager!

在 didbecomeActive 方法中:

func applicationDidBecomeActive(application: UIApplication) {
    if((locationManager) != nil)
    {
        locationManager.stopMonitoringSignificantLocationChanges()
        locationManager.delegate = nil
        locationManager = nil
    }
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.activityType = CLActivityType.OtherNavigation
    locationManager.requestAlwaysAuthorization()
    locationManager.startMonitoringSignificantLocationChanges()
}

如果我使用startUpdatingLocationdidUpdateLocations 方法会被调用,但不会被startMonitoringSignificantLocationChanges 调用。

为什么startMonitoringSignificantLocationChanges 没有被调用。 我正在ios模拟器中测试这个。我不知道如何签入设备。

【问题讨论】:

标签: ios swift ios-simulator cllocationmanager location-services


【解决方案1】:

它可以工作,但很难触发重大的位置变化——这通常发生在设备改变手机信号塔时——我认为模拟器不可能做到。

您可能必须骑上自行车/汽车并至少行驶几公里。

你可以使用一个技巧,它会触发重大的位置变化:

每隔几秒钟反复打开和关闭 iPhone 中的 Airplaine 模式,它应该会诱使设备认为它改变了手机信号塔并触发重大的位置变化。

【讨论】:

  • 是的,您必须行驶几公里 - 足以触发基站更换。
  • 记得给那个飞机按钮发送垃圾邮件:D 它应该在几次开关之后。
  • 非常感谢.. 救了我的命
  • 救命稻草 - 太棒了!
【解决方案2】:

在您的模拟器中,转到 Debug->Location->Custom 并更改位置,然后进行测试。

【讨论】:

    【解决方案3】:

    在您的模拟器中,选择Features -> Location -> Freeway Drive

    稍等startMonitoringSignificantLocationChanges触发didUpdateLocations

    【讨论】:

      【解决方案4】:

      注意: 只要设备从之前的通知移动 500 米或更远,应用程序就会收到通知。它不应期望通知的频率超过每五分钟一次。如果设备能够从网络检索数据,则位置管理器更有可能及时传递通知。 startMonitoringSignificantLocationChanges()

      【讨论】:

        猜你喜欢
        • 2014-10-22
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多