在后台请求iPhone位置? [英] Requesting iPhone location whilst in background?

查看:227
本文介绍了在后台请求iPhone位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题...我有一个应用程序,以30秒的间隔(使用NSTimer)记录用户位置,它工作正常,直到应用程序非活动,NStimer停止。因此,我正在寻找选项来保持我的位置间隔(30秒),同时仍然能够记录相当精确的位置数据(在100m的准确度内)。

Simple question ... I have an application that records a users location at 30second intervals (using an NSTimer) it works perfectly until the application goes "inactive" and the NStimer stops. As a consequence I am looking for options to maintain my location interval (30secs) whilst still being able to record fairly accurate location data (within 100m accuracy).


  • Option_001,强力:让CLLocationManager,startUpdatingLocation一直使用UIBackgroundModes =location运行。不推荐,排空电池。根据要求定制,精度约。 10-65米。

  • Option_001, Brute Force: Let CLLocationManager, startUpdatingLocation run all the time using UIBackgroundModes = "location". Not recommended, drains battery. Regularity upon request, Accuracy approx. 10-65m. Might just be the only realistic option.

Option_002,SLC:我可以使用重大位置更改,但位置更新的频率相当差(更不用说准确性)。如果应用程序在具有有限数量的蜂窝塔的农村或荒野地区运行,则尤其如此。规律性未知,准确性约。 500米

Option_002, SLC: I could use Significant Location Change but the frequency of location updates is pretty poor (not to mention accuracy). This is particularly true if the application is running in a rural or wilderness area with limited numbers of cell towers. Regularity unknown, Accuracy approx. 500m

Option_003,混合型:我可以在背景中使用重要位置更改(SLC)然后基于kCLLocationAccuracyBest请求GPS位置。这将工作,但SLC事件不会到达任何地方接近30秒的间隔(特别是在步行)。规律性未知,准确性约。 10-50米。

Option_003, Hybrid: I could use Significant Location Change (SLC) in the background as an indicator of "significant" movement and then request an GPS location based on kCLLocationAccuracyBest. This would work but the SLC events are not going to arrive at anywhere near 30second intervals (particularly when walking). Regularity unknown, Accuracy approx. 10-50m.

Option_004,还有什么?任何想法都会非常感激。

Option_004, Something else? any ideas would be much appreciated.

注意:我认为我的工作原因是在iPhone(通过USB连接)上按[LOCK],则调用applicationWillResignActive,但NSTimers不会停止。如果你尝试相同的iPhone未连接(即,因为手机将正常使用)NSTimers几乎立即停止applicationWillResignActive后调用。

NOTE: I thought I had this working because when you press [LOCK] on an iPhone (connected via USB) applicationWillResignActive is called but NSTimers do not stop. If you try the same with the iPhone un-connected (i.e. as the phone would be in normal use) the NSTimers stop almost immediately after applicationWillResignActive is called.

推荐答案

首先,不要使用计时器更新用户位置。从另一端接近:
检查,当接收到新位置时,自上次记录以来的间隔,并决定是否要记录新位置。

First of all, don't use a timer to update the user location. Approach it from the other end: check, when a new location is received, the interval since the last "recording" and decide if you want to record the new location or not.

此外,这将解决您的非活动状态问题。只需启用背景定位服务。 Info.plist>必需的背景模式>位置更新的应用程序注册

Also, this will get around your "inactive" state problem. Just enable background location services. Info.plist > Required background modes > App registers for location updates

当在后台接收到新位置时,应用程序将进入后台活动状态这将允许足够的时间进行API调用和推送新的位置。

Whilst in background, when a new location is received, your app will go in a "background active" state that will allow enough time to make an API call and push the new location.

在一句话中,你需要设计这个应用程序与新的背景模式很好地工作。

In a sentence, you need to design this app to work well with the new background modes.

注意:此解决方案不适用于iOS3.x

Note: this solution won't work for iOS3.x

这篇关于在后台请求iPhone位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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