Another use of block is to allow the program to run longer in the background. In the past, when the app was left on the home button, the app only had a maximum of 5 seconds to do some saving or cleanup work. But the app can call the UIApplication beginBackgroundTaskWithExpirationHandler
method, allowing the app to run for up to 10 minutes in the background. This time can be used to clean up the local cache, send statistics and other work.
The sample code that lets the program run long in the background is as follows:
AppDelegate.h file
@property (Assign, nonatomic) Uibackgroundtaskidentifier backgroundupdatetask;
APPDELEGATE.M file
-(void) Applicationdidenterbackground: (uiapplication *) application
{
[Self beingbackgroundupdatetask];
Add the code that you need to run long
[Self endbackgroundupdatetask];
}
-(void) Beingbackgroundupdatetask
{
Self.backgroundupdatetask = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{
[Self endbackgroundupdatetask];
}];
}
-(void) Endbackgroundupdatetask
{
[[UIApplication sharedapplication] endBackgroundTask:self.backgroundUpdateTask];
Self.backgroundupdatetask = Uibackgroundtaskinvalid;
}
IOS App Background Run