By default, when the app is left on the home key, the app has a maximum of 5 seconds to do some saving or cleanup work. However, the app can call the UIApplication beginBackgroundTaskWithExpirationHandler
method, allowing the app to run in the background for up to 10 minutes. This time can be used to clean up the local cache, send statistics and other time-consuming work.
AppDelegate.h file @property (assign, nonatomic) uibackgroundtaskidentifier backgroundupdatetask;//appdelegate.m file-( void) Applicationdidenterbackground: (uiapplication *) application{ [self beingbackgroundupdatetask]; Code that requires long-running [self endbackgroundupdatetask];} -(void) beingbackgroundupdatetask{ self.backgroundupdatetask = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{ [self endbackgroundupdatetask];} ];} -(void) endbackgroundupdatetask{ [[UIApplication sharedapplication] Endbackgroundtask: Self.backgroundupdatetask]; Self.backgroundupdatetask = Uibackgroundtaskinvalid;}
IOS running in the background