在swift 4中使用startMonitoringSignificantLocationChanges的正确方法是什么?

问题描述 投票:3回答:1

我尝试制作一个正在运行的应用程序,我想使用方法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 ...等。

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

任何的想法?

提前致谢。

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

ios core-location swift4
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.