2013-03-07 130 views

回答

3

您可以在后台通过使用此代码为特定的时间

UIBackgroundTaskIdentifier bgTask = 0; 
    UIApplication *app = [UIApplication sharedApplication]; 
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     [app endBackgroundTask:bgTask]; 
    }]; 
    self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self 
                 selector:@selector(startLocationServices) userInfo:nil repeats:YES]; 

执行任务,请参阅本: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

我认为这将帮助ü。 :)

+0

谢谢....我通过阅读苹果文档得出结论:上传过程可以在应用程序转到背景后执行最长10分钟, – 2013-03-07 09:46:55

+0

欢迎您@KhushbuPatel ..快乐编码:) – shivam 2013-03-07 10:29:56

1

应用程序可以请求在关闭后最多10分钟后在后台运行,以便它可以完成长时间运行的任务。只有一些进程被允许在后台运行。见实现长时间运行的后台任务部分in this reference.

如果您的应用允许的话,你可以试试下面的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application 

{ 

    UIBackgroundTaskIdentifier bgTask; 
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ 

     // Clean up any unfinished task business by marking where you 

     // stopped or ending the task outright. 

     [application endBackgroundTask:bgTask]; 

     bgTask = UIBackgroundTaskInvalid; 

    }]; 



    // Start the long-running task and return immediately. 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 



     // Do the work associated with the task, preferably in chunks. 



     [application endBackgroundTask:bgTask]; 

     bgTask = UIBackgroundTaskInvalid; 

    }); 

} 

,如果你想知道你的程序还剩多少时间来运行

NSTimeInterval ti = [[UIApplication sharedApplication]backgroundTimeRemaining]; 
NSLog(@"Remaining Time: %f", ti); // just for debug 

更多裁判去与这个reference PDF(page 60)

相关问题