Code Note: obtains the background time required by the system to complete the task.
I. Code.
AppDelegate. h
# Import <UIKit/UIKit. h> @ interface AppDelegate: UIResponder <UIApplicationDelegate> @ property (strong, nonatomic) UIWindow * window; // Add variable @ property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask; @ end
AppDelegate. m
# Import "AppDelegate. h "# import" RootViewController. h "@ implementation AppDelegate-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]; // Override point for customization after application launch. rootViewController * rootVC = [[RootViewController alloc] init]; UINavigat IonController * nav = [[UINavigationController alloc] initWithRootViewController: rootVC]; self. window. rootViewController = nav; self. window. backgroundColor = [UIColor whiteColor]; [self. window makeKeyAndVisible]; return YES;}-(void) applicationWillResignActive :( UIApplication *) application {// Sent when the application is about to move from active to inactive state. this can occur for certain types of te Mporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} -(void) applicationDidEnterBackground :( UIApplication *) application {[self beingBackground UpdateTask]; // Add the code you need to run for a long time. // Finally, complete the code again. [Self endBackgroundUpdateTask];}-(void) beingBackgroundUpdateTask {self. backgroundUpdateTask = [[UIApplication sharedApplication] balance: ^ {// you have to pay back it. [Self defined];}-(void) defined {[[UIApplication sharedApplication] endBackgroundTask: self. backgroundUpdateTask]; self. backgroundUpdateTask = UIBackgroundTaskInvalid ;}