模拟器测试不准确,需要真机测试
UIBackgroundTaskIdentifier _backgroundTask;
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[self beginBackgroundTask];
int count = 0;
while (YES) {
NSLog(@"%d",count);
count ++;
[NSThread sleepForTimeInterval:5];
}
[self endBackgroundTask];
}
- (void)beginBackgroundTask
{
_backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundTask];
}];
}
- (void)endBackgroundTask
{
[[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
_backgroundTask = UIBackgroundTaskInvalid;
}
本文讨论了模拟器测试的局限性,并强调了进行真机测试的必要性。通过一个iOS应用的示例,演示了如何使用UIKit框架来实现应用进入后台时的任务处理。


被折叠的 条评论
为什么被折叠?



