startMonitoringSignificantLocationChanges() 在物理设备上不起作用

startMonitoringSignificantLocationChanges() doesn't work on physical device

当 locationManager 设置为 startMonitoringSignificantLocationChanges() 时,我无法在物理设备上显示位置。

模拟器中一切正常,但是当我在 iPhone 上设置 运行 时,没有返回任何坐标,如果我将其更改为 startUpdatingLocation(),它可以在我的物理设备上运行。

知道哪里出了问题吗?

我的代码:

  let locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()

        startReceivingSingificantLocationChanges()
    }

    func startReceivingSingificantLocationChanges() {

        if !CLLocationManager.significantLocationChangeMonitoringAvailable() {
            //the service is not available
            print("Service is not available")
            return
        }

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startMonitoringSignificantLocationChanges()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first{
            print(location.coordinate)
            coordinatesLbl.text = "LAT:\(location.coordinate.latitude) LONG:\(location.coordinate.longitude)"
        }
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == CLAuthorizationStatus.denied{
            showLocationDisabledPopUp()
            print("didChangeAuthorization")
        }
    }

The documentationstartMonitoringSignificantLocationChanges() 状态:

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.

因此,在使用此方法时,您不应该经常期望它们。

startUpdatingLocation 更频繁地更新,这就是它起作用的原因。

我已经试过了,遇到了同样的问题。我最终将位置 desiredAccuracy 设置为 kCLLocationAccuracyKilometer。这具有给我显着位置变化的预期效果。另一种方法是使用 startUpdatingLocation 并在获得当前位置后,将位置管理器更改为 startMonitoringSignificantLocationChanges.