【发布时间】:2018-07-13 04:13:41
【问题描述】:
我有一个使用 CLLocationManager 请求位置更新的应用。当应用程序收到来自外部蓝牙设备的通知时,就会开始监控。应用程序在前台和后台都监听来自设备的通知,并且在这两种情况下都成功启动了位置更新。
如果在应用程序处于前台时开始监控,我会根据我在位置管理器上配置的距离过滤器获取位置更新。
但是,如果在应用程序处于后台时启动监控,我仍然会收到位置更新,但很少。有谁知道这是否是预期的行为,或者我是否可能错误地配置了某些东西?
代码中的设置如下:
fileprivate lazy var locationManager = CLLocationManager()
func initLocationManager(distanceFilter: CLLocationDistance = 270,
desiredAccuracy: CLLocationAccuracy = kCLLocationAccuracyNearestTenMeters,
activityType: CLActivityType = .automotiveNavigation) {
locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
if canGetLocationUpdate() {
locationManager.desiredAccuracy = desiredAccuracy
locationManager.distanceFilter = distanceFilter
locationManager.activityType = activityType
locationManager.delegate = self
}
}
func startLocationMonitoring() {
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
} else {
log.error("Unable to start location monitoring")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
log.debug("Got location \(location.coordinate) with speed \(location.speed) and accuracy \(location.horizontalAccuracy)")
}
}
Info.plist 中有以下内容:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key><string>To allow the app...</string>
<key>NSLocationAlwaysUsageDescription</key><string>To allow the app...</string>
<key>NSLocationWhenInUseUsageDescription</key><string>To allow the app...</string>
...
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>location</string>
</array>
【问题讨论】:
标签: ios swift cllocationmanager