我有一个开关,我在我的iPhone应用程序上打开,当我打开它并将其放在后台时,我在后台运行应用程序。
- (IBAction)doBackgroundEnableSwitch:(id)sender {
UISwitch *mySwitch = sender;
if (mySwitch.on) {
self.myBackgroundTaskID = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"WE RAN OUT OF TIME!");
//abort(); //Crash if we get here. (shouldn't be necessary because we ran out of time. I am purposely not
}];
NSLog(@"MyBackgroundTaskID = %lu", self.myBackgroundTaskID);
} else {
[[UIApplication sharedApplication]endBackgroundTask:self.myBackgroundTaskID];
}
}
当应用程序在后台运行时,使用NSLog我正在计算剩余时间。
- (void)displayApplicationState {
switch ([UIApplication sharedApplication].applicationState) {
case UIApplicationStateActive:
NSLog(@"UIApplicationStateActive");
break;
case UIApplicationStateBackground:
NSLog(@"UIApplicationStateBackground");
break;
case UIApplicationStateInactive:
NSLog(@"UIApplicationStateInactive");
break;
default:
break;
}
NSLog(@"timeRemaining: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
}
然而,当该计数达到0.00时,我的应用程序不会崩溃。 Apple文档称它应该崩溃。发生了什么事?
这是我的完整ViewController.h https://gist.github.com/RommelTJ/38560b273d4501f81439