后台任务iPhone [英] Background tasks iPhone

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

问题描述

我正在尝试基于计时器在后台实现位置更新。我知道iOS中不同的位置更新方法,并尝试过它们,但它们并不适合需要。我需要经常检查位置,即使没有活动。我尝试过使用计时器,但它们不在后台执行。这导致我发现这个问题如何在我的iOS应用程序中每n分钟更新一次后台位置?并提及

I am trying to implement location updates in the background based on a timer. I know all about the different location updating methods in iOS and have tried them but they don't fit the need. I need to check locations every so often even while not active. I've tried using timers but they don't execute in the background. That led me to find this question How do I get a background location update every n minutes in my iOS application? and it mentions using

UIApplication:beginBackgroundTaskWithExpirationHandler:

我的问题是你如何使用这种方法?我搜索过苹果文档,但是在处理背景执行和位置更新时没有示例,他们以递归方式指向对方。

My questions is how do you use this method? I've searched the apple docs but they are recursively pointing at each other when dealing with background execution and location updates with no examples.

我现在有一些基于某些代码的代码我找到的例子:

I have this code now based on some examples I've found:

- (void)applicationDidEnterBackground:(UIApplication *)application
{    
    locationUpdater=[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:locationUpdater];
        locationUpdater=UIBackgroundTaskInvalid;
    } ];
}

我在.plist中添加了关键字location以获得支持的背景模式

And I have added the key "location" in the .plist for supported background modes

添加这个只是保证我的应用程序不会被杀死一段时间,所以我可以保持计时器运行吗?或者我在哪里添加在后台模式下执行的方法。

Does adding this just guarantee that my application won't be killed for some certain amount of time so I can keep a timer running? or where do I add the methods that are to get executed while in the background mode.

推荐答案

如果你的后台任务运行时间长于10分钟后,您的应用将被终止。

If your background task runs longer than 10 minutes, your app will be terminated.

您确实应该使用os位置服务来获取有关位置更改的通知。为什么这还不够?如果位置没有改变,那么一遍又一遍地检索它的重点是什么,一旦它发生变化,你就会收到通知。

You really should use the os location services instead to get notified about location changes. Why is that not enough? If location doesn't change, what's the point of retrieving it over and over again, and once it changes, you'll be notified.

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

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