首页 > 移动平台 > 详细

ios实现程序切入后台,实现后台任务

时间:2014-08-13 01:19:45      阅读:397      评论:0      收藏:0      [点我收藏+]

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

    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

self->bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{

// Synchronize the cleanup call on the main thread in case

// the task catully finished at around the same time.

dispatch_async(dispatch_get_main_queue(), ^{

if (UIBackgroundTaskInvalid != self->bgTask) {

[application endBackgroundTask:self->bgTask];

self->bgTask = UIBackgroundTaskInvalid;

}

});

}];

// Start the long-running task and return immediately.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),

  ^{

  // Do the work assoicated with the task.

   

  // Synchronize the cleanup all on the main thread in case

  // the task catully finished at around the same time.

  dispatch_async(dispatch_get_main_queue(), ^{

   

                            NSLog(@"Background Time:%f",[[UIApplication sharedApplication] backgroundTimeRemaining]);

                           

  if (UIBackgroundTaskInvalid != self->bgTask) {

                               

  [application endBackgroundTask:self->bgTask];

  self->bgTask = UIBackgroundTaskInvalid;

  }

  });

  });

    

}

ios实现程序切入后台,实现后台任务,布布扣,bubuko.com

ios实现程序切入后台,实现后台任务

原文:http://blog.csdn.net/qi_ruihua/article/details/38522063

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!