0%

iOS APP后台运行时间探究及延长

实验设备:iPhone7plus iOS12.1

问题:发现市面上很多APP进入后台后,过了16min甚至20min后,再次点击APP,都能够保持在原来的页面.而我们自己的APP,进入后台过个三分钟就被kill掉了,再次进入都是重新启动.
网易新闻APP,主站APP,喜马拉雅APP都不会.为什么我们的APP在挂起后会这么快被系统从内存中清理?而别的APP却不会.

目前app,没有做任何特别处理,申请的权限也很常规,如下:

APP后台模式

解决:经过不断的分析,最终发现是代码中在向系统申请后台运行时间错误使用导致,在调用beginBackgroundTaskWithExpirationHandler后,没有配对调用endBackgroundTask:.

吐槽:有时候工程一大,出现一些BUG真的很难分析原因.

向系统申请后台运行时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- (void)backgroundDoSomethings {
UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)];
__block UIBackgroundTaskIdentifier 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.
[self doSomethingWithCompletionBlock:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}

- (void)doSomethingWithCompletionBlock:(void (^)(void))completion
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSLog(@"开始睡");
sleep(175); //200x 170√ 180x 175√
NSLog(@"睡醒");

if (completion) {
completion();
}
});
}

实测运行的时间大致在175s左右,小于180s.即通过beginBackgroundTaskWithExpirationHandler:方法可以向系统申请大约180s的后台运行时间.

相关方法:

1
2
3
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^ __nullable)(void))handler  NS_AVAILABLE_IOS(4_0) NS_REQUIRES_SUPER;
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithName:(nullable NSString *)taskName expirationHandler:(void(^ __nullable)(void))handler NS_AVAILABLE_IOS(7_0) NS_REQUIRES_SUPER;
- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)identifier NS_AVAILABLE_IOS(4_0) NS_REQUIRES_SUPER;

特别注意:
beginBackgroundTaskWithExpirationHandler一定要与endBackgroundTask配对使用,否则时间到后系统将直接kill掉你的APP.

如何延长APP后台运行时间?甚至无限长.

以后有空研究.

觉得文章有帮助可以打赏一下哦!