[英]How to get location when app goes to background or phone is locked?
I'm trying using locationManager
to get my location and update it to my webservice. 我正在尝试使用
locationManager
获取我的位置并将其更新到我的Web服务。
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
//some code here
}
It's normal, but if my app goes to the background or phone screen is locked, updating location may be paused. 这是正常现象,但是如果我的应用进入后台或手机屏幕被锁定,则更新位置可能会暂停。 How do I get the location anyway?
我如何获得位置?
Read the full blog post on it: iPhone Background GPS: accurate to 500m, not enough for foot traffic 阅读完整的博客文章: iPhone背景GPS:精确到500m,不足以进行人流
Here is a tutorial: iOS Multitasking: Background Location 这是一个教程: iOS多任务处理:后台位置
I recommend you set up a handler such as this example: 我建议您设置一个处理程序,例如以下示例:
TTLocationHandler TTLocationHandler
Configure the handler for the recency and accuracy required. 针对所需的新近度和准确性配置处理程序。 Set up a listener for the notification "LocationHandlerDidUpdateLocation".
为通知“ LocationHandlerDidUpdateLocation”设置一个侦听器。 Put your code to upload the data in a method called on receipt of that notification.
将您的代码放在一种在收到通知后称为的方法中上传数据。
If you do not require fine grain detail, I would advise you use the battery saving options of significantLocationChange monitoring in background. 如果您不需要细粒度的细节,我建议您在后台使用有意义的LocationChange监视的省电选项。 In that case you will not need to add background mode in info.plist and you will not waste resources unnecessarily.
在这种情况下,您无需在info.plist中添加后台模式,也不会不必要地浪费资源。 The app will be awakened and your method called only on significant movement of the user.
该应用程序将被唤醒,并且仅在用户大量移动时才调用您的方法。 There is configurable options in the handler to require constant monitoring in background and/or foreground, and to set recency and accuracy thresholds.
处理程序中有可配置的选项,要求对背景和/或前景进行持续监视,并设置新近度和准确性阈值。
See the LMPinTracker class for example of how to respond and save your data to web or locally. 有关如何响应并将数据保存到Web或本地的示例,请参见LMPinTracker类。 See the thread Invoke get current coordinates every few seconds without NSTimer for discussion of use.
有关使用的讨论,请参见线程调用每隔几秒钟即可获取当前坐标而无需使用NSTimer的情况。
Add This Key to your Application Plist 将此密钥添加到您的应用程序列表
Required background modes
Mark it as Array, and on its item0
add this Value 将其标记为数组,并在其
item0
添加此值
App registers for location updates
The app will now get location data in Background too. 该应用程序现在还将在后台获取位置数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.