在应用程序处于后台时,启动位置监控时,iOS位置更新很少发生

在应用程序处于后台时,启动位置监控时,iOS位置更新很少发生,第1张

概述我有一个使用CLLocationManager来请求位置更新的应用程序.当应用程序收到外部蓝牙设备的通知时,将启动监控.当应用程序处于前台和后台时,应用程序将侦听来自设备的通知,并且在两种情况下都会成功启动位置更新. 如果在应用程序位于前台时启动监控,则会根据我在位置管理器上配置的距离过滤器获取位置更新. 但是,如果在应用程序处于后台时启动监控,我仍然会获得位置更新,但很少.有谁知道这是否是预期的 我有一个使用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>
解决方法 来自苹果文档

If your iOS app must keep monitoring location even while it’s in the background,use the standard location service and specify the location value of the uibackgroundmodes key to continue running in the background and receiving location updates. (In this situation,you should also make sure the location manager’s pausesLocationUpdatesautomatically property is set to YES to help conserve power.) Examples of apps that might need this type of location updating are fitness or turn-by-turn navigation apps.

似乎iOS限制后台位置更新以节省设备电源.

总结

以上是内存溢出为你收集整理的在应用程序处于后台时,启动位置监控时,iOS位置更新很少发生全部内容,希望文章能够帮你解决在应用程序处于后台时,启动位置监控时,iOS位置更新很少发生所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099330.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存