【发布时间】: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