[UIApplication backgroundTimeRemaining]
The amount of time the application has to run in the background. (read-only)
@property(nonatomic, readonly) NSTimeInterval backgroundTimeRemaining
Discussion of [UIApplication backgroundTimeRemaining]
This property contains the amount of time the application has to run in the background before it may be forcibly killed by the system. While the application is running in the foreground, the value in this property remains suitably large. If the application starts one or more long-running tasks using the
beginBackgroundTaskWithExpirationHandler:
method and then transitions to the background, the value of this property is adjusted to reflect the amount of time the application has left to run.
Example of [UIApplication backgroundTimeRemaining]
- (void)backgroundHandler {
NSLog(@"### -->VOIP backgrounding callback");
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (1) {
NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
[self doSomething];
sleep(1);
}
});
Example of [UIApplication backgroundTimeRemaining]
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier taskID;
taskID = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:taskID];
}];
{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier taskID;
taskID = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:taskID];
}];
if (taskID == UIBackgroundTaskInvalid) {
NSLog(@"Failed to start background task!");
return;
}
NSLog(@"Starting background task with %f seconds remaining", app.backgroundTimeRemaining);
[NSThread sleepForTimeInterval:60]; ///60 seconds, MAX 600s
NSLog(@"Finishing background task with %f seconds remaining",app.backgroundTimeRemaining);
NSLog(@"Failed to start background task!");
return;
}
NSLog(@"Starting background task with %f seconds remaining", app.backgroundTimeRemaining);
[NSThread sleepForTimeInterval:60]; ///60 seconds, MAX 600s
NSLog(@"Finishing background task with %f seconds remaining",app.backgroundTimeRemaining);
[app endBackgroundTask:taskID];
}
}