美文网首页
IOS 无限后台

IOS 无限后台

作者: Miro丶K | 来源:发表于2016-11-14 09:58 被阅读0次

    - (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;

    }

    相关文章

      网友评论

          本文标题:IOS 无限后台

          本文链接:https://www.haomeiwen.com/subject/cckcuttx.html