您需要使用beginBackgroundTaskWithExpirationHandler
来处理您的进程。使用此代码。
在这里,我呼吁来自applicationDidEnterBackground此方法:(UIApplication的*)应用程序存储SQLite中,即使该应用程序进入后台或UIApplicationStateInactive(按下电源按钮)的数据。如果application.backgroundTimeRemaining变为0,保存过程将持续10分钟,根据IOS标准。我将发布一个本地通知,将我的应用程序启动到前台以保持我的进程活着。使用此代码并自定义您的过程。
-(void)handleBackgroundSavingingProcess:(UIApplication *)application
{
NSLog(@"background task remaining time before background %f",application.backgroundTimeRemaining);
if ((([application applicationState] == UIApplicationStateBackground) || ([application applicationState] == UIApplicationStateInactive)) && [application respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)])
{
self.bgTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
// Synchronize the cleanup call on the main thread in case
// the task actually finishes at around the same time.
NSLog(@"background task remaining time in expiration handler %f",application.backgroundTimeRemaining);
dispatch_queue_t concurrentQueue;
if([[[UIDevice currentDevice] systemVersion] doubleValue] >= 5.0)
concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0UL);
else
concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0UL);
if (concurrentQueue == nil)
return;
dispatch_async(concurrentQueue, ^{
if (self.bgTaskId != UIBackgroundTaskInvalid){
NSLog(@"background task remaining time in dispatch queue %f",application.backgroundTimeRemaining);
NSLog(@" Downloading status %d",self.isDownloadingInComplete);
if(self.isDownloadingInComplete)
{
[application presentLocalNotificationNow:localNotification];
NSLog(@"Local notification fired.");
}
[DataManager managedObjectContext] save:nil];
[application endBackgroundTask:self.bgTaskId];
self.bgTaskId = UIBackgroundTaskInvalid;
NSLog(@"Initialization invalid.");
}
});
}];
}
}
首先,你不能强迫你的应用程序在前台。 – viral 2013-03-11 09:34:42
您可以将数据保存在用户默认值中,直到数据未保存,然后清除用户默认值 – channi 2013-03-11 09:40:59
这不仅仅是将数据保存在用户默认值中,我将数据保存为plist,我也使用Coredata,事情是如果用户试图关闭设备,我需要一个点来保存所有这些信息。 – user1708257 2013-03-11 09:45:51