有没有一种情况,当软件退出后,你还是需要运行一点东西,或者是需要很长时间来运行的一个代码。此时,就需要向后台申请时间了。但是官司方资料。
根据苹果文档中关于后台执行的描述,任何app都有10分钟左右的后台任务执行时间。 10分钟后,app会被iOS强行挂起。
但是,有5类app允许有“无限的”后台运行时间:
1. Audio。
2. Location/GPS。
3. VoIP。
4. Newsstand。
5. Exernal Accessory 。
你可以将任何app声明为上述5种类型以获得无限的后台运行时间,但当你提交app到App Store时,苹果会审查你的app,一旦发现你“滥用”了后台API,你的app将被拒绝。
当然,对于企业开发而言,不存在“滥用”的问题——企业app可以通过OTA部署,不经过苹果商店审查。
当一个iOS应用被送到后台,它的主线程会被暂停。你用NSThread的detachNewThreadSelector:toTar get:withObject:类方法创建的线程也被挂起了。如果你想在后台完成一个长期任务,就必须调用UIApplication的beginBackgroundTaskWithExpirationHandler:实例方法,来向iOS借点时间。UIApplication的backgroundTimeRemaining属性包含了程序完成他的任务可以使用的秒数。如果在这个期限内,长期任务没有被完成,iOS将终止程序。每个对beginBackgroundTaskWithExpirationHandler:方法的调用,必须要相应的调用endBackgroundTask:方法(UIApplication的另一个实例方法)。也就是说,如果你向iOS要更多时间来完成一个任务,你必须告诉iOS你什么时候能完成那个任务。
好吧,不多说。上代码。一共有2种情况。
1种是只申请程序要运行完成需要的时间。1种是申请无限时间,可以在控制在前台运行,还是后台运行,或者是前后台运行。
第一种,申请程序要完成所需要的时间。
.h
//添加变量 @property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;
.m
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self beingBackgroundUpdateTask];
// 在这里加上你需要长久运行的代码
//最后彻底的还一次。
[self endBackgroundUpdateTask];
}
- (void)beingBackgroundUpdateTask
{
self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
//有借有还。
[self endBackgroundUpdateTask];
}];
}
- (void)endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}
第2种,是可以控制在前台,在后台,或者在前台和后台都能运行。
.h
@property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier backgroundTaskIdentifier; @property (nonatomic, strong) NSTimer *myTimer;
.m
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// 使用这个方法来释放公共的资源、存储用户数据、停止我们定义的定时器(timers)、并且存储在程序终止前的相关信息。
// 如果,我们的应用程序提供了后台执行的方法,那么,在程序退出时,这个方法将代替applicationWillTerminate方法的执行。
// 标记一个长时间运行的后台任务将开始
// 通过调试,发现,iOS给了我们额外的10分钟(600s)来执行这个任务。
self.backgroundTaskIdentifier =[application beginBackgroundTaskWithExpirationHandler:^(void) {
// 当应用程序留给后台的时间快要到结束时(应用程序留给后台执行的时间是有限的), 这个Block块将被执行
// 我们需要在次Block块中执行一些清理工作。
// 如果清理工作失败了,那么将导致程序挂掉
// 清理工作需要在主线程中用同步的方式来进行
[self endBackgroundTask];
}];
// 模拟一个Long-Running Task
self.myTimer =[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(timerMethod:) userInfo:nil
repeats:YES];
}
- (void) endBackgroundTask{
dispatch_queue_t mainQueue = dispatch_get_main_queue();
AppDelegate *weakSelf = self;
dispatch_async(mainQueue, ^(void) {
AppDelegate *strongSelf = weakSelf;
if (strongSelf != nil){
[strongSelf.myTimer invalidate];// 停止定时器
// 每个对 beginBackgroundTaskWithExpirationHandler:方法的调用,必须要相应的调用 endBackgroundTask:方法。这样,来告诉应用程序你已经执行完成了。
// 也就是说,我们向 iOS 要更多时间来完成一个任务,那么我们必须告诉 iOS 你什么时候能完成那个任务。
// 也就是要告诉应用程序:“好借好还”嘛。
// 标记指定的后台任务完成
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
// 销毁后台任务标识符
strongSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
});
}
// 模拟的一个 Long-Running Task 方法
- (void) timerMethod:(NSTimer *)paramSender{
// backgroundTimeRemaining 属性包含了程序留给的我们的时间
NSTimeInterval backgroundTimeRemaining =[[UIApplication sharedApplication] backgroundTimeRemaining];
if (backgroundTimeRemaining == DBL_MAX){
//前台打印
NSLog(@"Background Time Remaining = Undetermined");
} else {
//后台打印
NSLog(@"Background Time Remaining = %.02f Seconds", backgroundTimeRemaining);
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
//添加此段代码,则在前台就不运行了。否则会前后台一起运行。除第一次启动的时候,是前台不运行,退出后台时候运行。
if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid){
[self endBackgroundTask];
}
}
获取系统完成任务所需要的后台时间,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/yang-guang-girl/p/3797126.html