首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS -- NSTimer的后台任务是否仍然受到苹果的限制?

iOS -- NSTimer的后台任务是否仍然受到苹果的限制?
EN

Stack Overflow用户
提问于 2013-03-09 05:46:47
回答 1查看 1.1K关注 0票数 1

我知道我在抽签,但希望能得到答案。

我有一个应用程序,需要在后台与服务器通信,无需用户干预。推送通知到达或至少定期(每5分钟左右)。

我可以潜在的螺栓位置更新,并将其发送到服务器。我甚至可以证明使用粗糙的位置是合理的,所以苹果批准了它。最糟糕的情况是,我将支付299美元购买企业开发人员许可证,并使用本地发行版(是我唯一关心的)。

我最后的希望是用NSTimer定期发送一个后台任务。在允许的背景任务类型上,使用这种方法的应用程序是否仍然受苹果的限制?(媒体/地点/voip/报摊/外部设备)似乎如此歧视性.

PS。该应用程序现在工作在ipod上的开发模式,包括与服务器的定期通信。就背景模式而言,我还没有在plist中声明任何内容。这是不是意味着我没事?

EN

回答 1

Stack Overflow用户

发布于 2013-03-09 06:08:47

昨天,我实现了以下功能,并获得了苹果的批准。我的应用程序做不到(媒体/位置/voip/报摊/外部设备)

通过使用applicationDidEnterBackground中的以下代码,您可以获得600秒(10分钟)的最大时间:

代码语言:javascript
复制
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
__block UIBackgroundTaskIdentifier background_task; //Create a task object
background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
    [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
    background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
    //System will be shutting down the app at any point in time now
}];
//Background tasks require you to use asyncrous tasks
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //Perform your tasks that your application requires
    NSLog(@"\n\nRunning in the background!\n\n");
    [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
    background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
});
}

}

文档可以在这里找到Doc/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

我刚刚实现了backgroundTaskIdentifier对象,并使background_task失效以检查时间,应用程序是活动的,运行了600秒。您甚至可以通过以下方法获得剩余的时间

代码语言:javascript
复制
NSLog(@"Time remaining: %f", application.backgroundTimeRemaining);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15307674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档