beginBackgroundTaskWithExpirationHandler:
Marksthe beginning of anew long-running background task.
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^)(void))handler
Parameters
handler
A handler to be called shortly before the application’s remaining background time reaches 0. You should use this handler to clean up and mark the end of the background task. Failure to end the task explicitly will result in the termination of the application. The handler is called synchronously on the main thread,thus blocking the application’s suspension momentarily while the application is notified.
Return Value
A unique identifier for the new background task. You must pass this value to the endBackgroundTask:method to mark the end of this task. This method returnsU IBackgroundTaskInvalid (page 45) if running inthe background is not possible.
Discussion
This method lets your application continue to run for a period of time after it transitions to the background.You should call this method at times where leaving a task unfinished might be detrimental to your application’s user experience. For example, your application could call this method to ensure that had enough time to transfer an important file to a remote server or at least attempt to make the transfer and note any errors. You should not use this method simply to keep your application running after it moves to the background.
Each call to this method must be balanced by a matching call to theendBackgroundTask:method.Applications running background tasks have a finite amount of time in which to run them. (You can find outhow much time is available using the backgroundTimeRemaining(page 12) property.) If you do not call endBackgroundTask:for each task before time expires, the system kills the application. If you provide ablock object in thehandlerparameter, the system calls your handler before time expires to give you a chanceto end the task.
You can call this method at any point in your application’s execution. You may also call this method multipletimes to mark the beginning of several background tasks that run in parallel. However, each task must beended separately. You identify a given task using the value returned by this method.
This method can be safely called on a non-main thread.
2012-12-13 | © 2012 Apple Inc. All Rights Reserved.20
UIApplication Class Reference
Instance Methods
Availability
Available in iOS 4.0 and later.
See Also
– endBackgroundTask:(page 25)Related Sample Code
AVCam
Declared in
UIApplication.h