简体   繁体   English

应用程序进入后台或手机锁定时如何获取位置?

[英]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? 我如何获得位置?

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.

相关问题 进入后台模式时,如何在应用中更新位置管理器和计时器? - How can i update location manager and a timer in my app when it goes to background mode? 当应用程序进入后台时如何运行NSThread - how to run NSThread when app goes to background 当应用程序进入后台时,如何创建“返回应用程序”状态栏? - How to create a “return to app” status bar when app goes to the background? 应用程序进入后台时如何取消Everyplay SDK录制? - How to cancel Everyplay SDK recording when app goes to background? 当应用程序进入后台状态时如何取消AVExportSession? - How to Cancel AVExportSession when app goes to background state? 应用程序在后台运行时如何保持运行NSTimer - how to keep running NSTimer when app goes in background 当应用程序在后台时如何停止位置服务? - How to stop location services when the app is in background? 当应用程序转到后台时,AVAudioPlayer无法播放 - AVAudioPlayer does not play when app goes to background 当应用程序进入后台时,网络连接正在丢失 - Network connection is losing when the app goes to the background 当应用程序进入后台时,MPMoviePlayerViewController消失 - MPMoviePlayerViewController gets dissmiss when app goes background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM