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

- (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实现程序切入后台,实现后台任务,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。