- (void)applicationBackgrountHandler:(UIApplication *)application {
if (self.background_task == UIBackgroundTaskInvalid) {
self.background_task = [application beginBackgroundTaskWithExpirationHandler:nil];
__weak __typeof(&*self)weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while(1) {
if (!weakSelf.backgroundActive) {
[application endBackgroundTask:weakSelf.background_task];
weakSelf.background_task = UIBackgroundTaskInvalid;
break;
}
//编写执行任务代码
NSLog(@"%@", [NSDate date]);
sleep(1);
}
});
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
self.backgroundActive = YES;
[self applicationBackgrountHandler:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
self.backgroundActive = NO;
}