startMonitoringSignificantLocationChanges 在 swift 中不起作用

startMonitoringSignificantLocationChanges not working in swift

我在 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()
}

如果我使用 startUpdatingLocation,会调用 didUpdateLocations 方法,但不会调用 startMonitoringSignificantLocationChanges

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

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

您可能必须搭乘 bike/car 并至少行驶几公里。

不过,您可以使用一个技巧,它会触发重大的位置更改:

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

在您的模拟器中,转到“调试”->“位置”->“自定义”并更改位置,然后进行测试。

Note: Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner. startMonitoringSignificantLocationChanges()

在您的模拟器中,select 功能 -> 位置 -> 高速公路行驶

等待 startMonitoringSignificantLocationChanges 触发 didUpdateLocations