iOS - 使用NSTimer安排后台任务是否仍受Apple限制?

时间:2013-03-09 05:46:47

标签: ios background nstimer

我知道我正在拉吸管,但希望得到答案。

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

我可以通过螺栓固定位置更新并将其发送到服务器。我甚至可以证明使用粗略位置是合理的,因此Apple批准了它。最糟糕的情况是,我将支付299美元购买企业开发人员许可证并使用本地分发(我唯一关心的)。

我最后的希望是使用NSTimer定期发送后台任务。 使用此方法的应用程序是否仍然受到Apple允许的后台任务类型限制? (媒体/位置/ voip /报亭/外部设备)它似乎是如此歧视......

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

1 个答案:

答案 0 :(得分:1)

我已实施以下内容,并于昨天获得苹果批准的应用程序。我的应用没有(媒体/位置/ voip /报亭/外部设备)

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

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
});
}

}

可在此处找到文档http://disanji.net/iOS_Doc/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

我刚刚实现了backgroundTaskIdentifier对象并使background_task无效以检查时间,应用程序处于活动状态且运行600秒。您甚至可以使用此

获得剩余时间
NSLog(@"Time remaining: %f", application.backgroundTimeRemaining);