iPhone:无限期地在后台运行
我正在寻找一种方法让 iPhone 应用程序无限期地在后台运行。该应用程序不会使用位置服务或 VoIP,而是执行特定任务(抱歉,但我无法明确说明任务是什么)。该任务类似于检查文件是否存在,然后休眠 1 秒。就这个问题而言,任务应该永远运行。
任何帮助将不胜感激。据我从 iPhone 文档中可以看出,我所要求的是不可能的,所以我很困惑......
I am looking for a way to have an iPhone application run in the background indefinitely. This app will not be using location services or VoIP, but instead be preforming a specific task (sorry but I cannot explicitly say what the task is). The task can be comparable to checking to see if a file exists and then sleeping for 1 second. For the purpose of this question, the task should be running forever.
Any help would be appreciated. As far as I can tell from iPhone documentation what I am requesting is not possible, so I am rather stumped...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你是对的 - 除非你在后台运行的原因符合 Apple 定义的类别之一(播放音频、VoIP 等),否则你的应用程序将不会被允许在后台无限期运行。如果您想要这样做的原因确实令人信服,您可能需要与 Apple 讨论让他们允许这样做。不过,预计这是一场漫长的艰苦战斗。更务实的方法是寻找可以实现相同效果的方法,例如在服务器上运行后台代码或注册来自系统的各种通知。
You're right -- unless your reason for running in the background fits within one of the categories that Apple has defined (playing audio, voip, etc.), your app won't be allowed to run indefinitely in the background. If the reason you want to do that is really compelling, you might want to talk to Apple about getting them to allow it. Expect a long uphill battle, though. A more pragmatic approach would be to look for ways that you can achieve the same effect, like running your background code on a server or registering for various notifications from the system.
当前的 SDK 无法完成此操作。后台应用程序只能使用特定服务(音频、位置……)。
This cannot be done with the current SDK. Apps on the background can only use specific services (audio, location, ...).
此处描述了 iOS 上的多任务处理。 http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH5
允许的多任务处理类型有
audio
、location
和voip
,所以听起来像该任务在 iOS 上可能是不可能的。Multitasking on iOS is described here. http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH5
The permissible types of multitasking are
audio
,location
, andvoip
, so it sounds like your task may be impossible on iOS.这只能在越狱环境中完成。如果这不适合您,那么 SDK 目前不支持长期后台活动,除非它用于音频、位置或 VoIP 目的。
This can only be done on a jailbroken environment. If that's not an option for you, than the SDK does not currently support long-term background activity without it being for audio, location, or voip purposes.
正如已经讨论过的,真正的后台处理将无法满足您的要求。
但是,也许您可以利用后台通知来达到您的目的?
As already discussed true background processing will not be possible for your requirements.
However, maybe you could make use of background notifications to achieve your ends?
这是一个令人失望的问题,但我仍然回答你只是提供了逻辑。尽管在应用商店审核过程中这是不可接受的。
while(您的条件)
{
NSAutoreleasePool *aPool = [[NSAutoreleasePool alloc] init];
[aPool release]
}
在
applicationDidEnterBackground
中调用此函数并查看仪器上发生的情况,以充分了解后台工作。快乐编码!
A disappointing question but still I am answering you for just providing the logic. Although its not acceptable during app store review process.
while (your condition)
{
NSAutoreleasePool *aPool = [[NSAutoreleasePool alloc ] init];
[aPool release]
}
Call this in
applicationDidEnterBackground
and see whats happening at the instruments to get a good knowledge of background working.Happy Coding!