简体   繁体   English

在iOS 7中从后台更新位置?

[英]Updating location from the background in ios 7?

I have an iOS app that use the location services. 我有一个使用定位服务的iOS应用。

I need to call StartUpdatingLocation() while the app is in the background at a specific time (ie 4/17/2014 at 3:00 pm). 我需要在特定时间(即2014年4月17日下午3:00)在后台运行应用程序时调用StartUpdatingLocation() )。

Previously I tried using a timer to do this, but found out that you can't run a timer while the app is in the background on iOS 7. Currently I'm running the location services when the app is initially booted and slowly updating it every so often. 以前,我尝试使用计时器来执行此操作,但是发现当应用程序在iOS 7上处于后台运行时,您无法运行计时器。当前,我在启动应用程序并缓慢更新时正在运行位置服务。每隔一段时间。 With each update, I check if the specific start time has been reached yet and if so I start logging the coordinates to the database. 每次更新时,我都会检查是否已达到特定的开始时间,如果是,则开始将坐标记录到数据库中。

Is there a more efficient/less data and battery intensive way to have location services start from the background of an iOS 7 application? 是否有更高效/更少的数据和电池消耗的方法来使定位服务从iOS 7应用程序的后台启动?

I've also considered using LocationUpdatesPaused and LocationUpdatesResumed events to help reduce the battery and data usage, while checking for my specific date and time. 我还考虑过使用LocationUpdatesPausedLocationUpdatesResumed事件来帮助减少电池和数据使用量,同时检查我的特定日期和时间。

I've consulted this helpful link: http://gooddevbaddev.wordpress.com/2013/10/22/ios-7-running-location-based-apps-in-the-background/ 我已咨询了此有用的链接: http : //gooddevbaddev.wordpress.com/2013/10/22/ios-7-running-location-based-apps-in-the-background/

You can't do this. 你做不到

Apple explicitly state in their documentation you should not call StartUpdatingLocation() while in the background. Apple在其文档中明确声明您不应在后台调用StartUpdatingLocation()。 Apparently you used to be able to do this and it worked before iOS7 but I tried it with iOS7 and it doesn't work, so if it used to be possible it now no longer is. 显然,您曾经能够做到这一点,并且它在iOS7之前就可以使用,但是我在iOS7上尝试过,但是它不起作用,因此,如果以前有可能,现在不再可用。

But apart from that, with iOS there is no way of scheduling an action at a particular time in the background to occur anyway. 但除此之外,iOS无法将活动安排在后台的特定时间进行。

What you could do is change the accuracy to low (in combination with pausing updates etc.) when you don't need location updates and set it to high when you do to preserve battery life, but you cannot do this at a specific time when in the background. 您可以做的是在不需要位置更新时将准确度更改为低(结合暂停更新等),并在保存电池寿命时将其设置为高,但是在特定时间不能执行此操作在后台。

iOS supports background location services as long as you declare them in your info.plist file like so: 只要您在info.plist文件中声明后台位置服务 ,iOS就会支持它,如下所示:

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

However, Apple is very fussy about the types of app it will approve with this behaviour because of battery life implications. 但是,由于电池寿命的影响,苹果公司对于这种行为批准的应用程序类型非常挑剔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM