iOS下有限后台任务如何使用呢?
iOS应用,在切换到后台时,可以开启有限后台任务。
见苹果开发文档,
一.几个相关方法
二.切换到后台的处理
1
2
3
4
5
6
7
8
9
10
11
| - (void)applicationDidEnterBackground:(UIApplication *)application {
self.backgroundTaskIdentifier =
[application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];
//添加自己的处理逻辑
}
|
三.切换到前台的处理
1
2
3
4
5
6
7
8
| - (void)applicationWillEnterForeground:(UIApplication *)application {
if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid){
[application endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
}
|