發表文章

目前顯示的是 4月, 2015的文章

iOS 進入Background後可執行時間,可額外要求的時間由10分鐘變成3分鐘?

在不同的文件中看到iOS App由前景進入背景後,只能再執行10秒或者5秒。然後就會被suspended! 之前iOS 7的書籍中提到可以再額外要求10分鐘,當然現在iOS已經到8的版本了,今天試了一下,發現 好像可額外要求的時間又被Apple給縮短了! 建一個簡單的single view App, 其ViewController.m內容: #import "ViewController.h" @interface ViewController () { NSDateFormatter *format; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; format = [[NSDateFormatter alloc] init]; [format setDateFormat:@"H:m:s"]; [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(ticker:) userInfo:nil repeats:YES]; } - (void)ticker:(NSTimer *)theTimer { NSDate *today = [NSDate date]; NSLog(@"%@", [format stringFromDate:today]); NSLog(@"%f", [UIApplication sharedApplication].backgroundTimeRemaining); } @end 在AppDelegate.m中加入以下程式: - (void)endBackgroundTask { [[UIApplication sharedApplication] endBackgroundTask:bgTask]; } - (void)applicationDidEnterBackground:(UIApplication *)application { bgTask = [application begi