用户重新启动手机后,如何继续跟踪位置? [英] How to continue tracking location after the user restarts the phone?

查看:95
本文介绍了用户重新启动手机后,如何继续跟踪位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要将位置数据发布到我的后端.我已将位置权限设置为always,并将pausesLocationUpdatesAutomatically设置为false,并将CLLocationManager设置为false,并且即使应用程序在后台或重新启动电话后,也需要继续跟踪电话的位置.

My application need to post the location data to my backend. I have set the location permissions to always and set the pausesLocationUpdatesAutomatically to false for CLLocationManager and need to continue tracking the phones location even when the application is in background or after the phone is restarted.

当应用程序处于后台模式时,我可以使其正常运行.但是,当手机重新启动后,它将停止工作.

I'm able to make it work when the application is in background mode. But it stops working when the phone is restarted.

我该怎么做?

顺便说一句,我知道

推荐答案

Apple文档对此进行了很好的记录. 只有使用"startMonitoringSignificantLocationChanges"启动位置管理器,您才能获得它.

This is WELL documented by Apple documentations. Only if Location manager is started with "startMonitoringSignificantLocationChanges" you can get it.

例如:

1)在App委托中启动一个Singleton GeoLocationManager.shared.locationManager,因此,如果重新启动,它将开始运行.

1) in App delegate start a singleton GeoLocationManager.shared.locationManager, so if you restart will be running.

2)在回叫中:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
{

更新您的位置

如果崩溃,它将重新进入. 我认为您已经设置了il plist:

it will be re-entrant if crashes. I think You already set il plist:

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

注意:由于ios9是强制性的,因此添加了用于隐私的代码.

note: add code for privacy, as since ios9 is mandatory..

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
...
}

在plist中:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Needed to track on maps even if background</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Needed to track on maps even if background</string>

这篇关于用户重新启动手机后,如何继续跟踪位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆