你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> ios實現程序切入後台,實現後台任務

ios實現程序切入後台,實現後台任務

編輯:關於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;

}

});

});

}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved