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 beginBackgroundTaskWithExpirationHandler:^{
// will enter this block after 10 min.
[self endBackgroundTask];
}];
}


在iPhone(iOS 8)手機上執行, 輸出:
2015-04-14 16:48:26.402 test_bg10[1520:601603] 16:48:26
2015-04-14 16:48:26.405 test_bg10[1520:601603] 178.710931
2015-04-14 16:48:31.498 test_bg10[1520:601603] 16:48:31
2015-04-14 16:48:31.500 test_bg10[1520:601603] 173.615327
2015-04-14 16:48:36.498 test_bg10[1520:601603] 16:48:36
2015-04-14 16:48:36.500 test_bg10[1520:601603] 168.615193
2015-04-14 16:48:41.498 test_bg10[1520:601603] 16:48:41
2015-04-14 16:48:41.501 test_bg10[1520:601603] 163.614815
2015-04-14 16:48:46.498 test_bg10[1520:601603] 16:48:46
2015-04-14 16:48:46.501 test_bg10[1520:601603] 158.614851
2015-04-14 16:48:51.501 test_bg10[1520:601603] 16:48:51
2015-04-14 16:48:51.503 test_bg10[1520:601603] 153.612953
2015-04-14 16:48:56.501 test_bg10[1520:601603] 16:48:56
2015-04-14 16:48:56.504 test_bg10[1520:601603] 148.612045
2015-04-14 16:49:01.501 test_bg10[1520:601603] 16:49:1
2015-04-14 16:49:01.503 test_bg10[1520:601603] 143.612885
2015-04-14 16:49:06.498 test_bg10[1520:601603] 16:49:6
2015-04-14 16:49:06.500 test_bg10[1520:601603] 138.615272
2015-04-14 16:49:11.501 test_bg10[1520:601603] 16:49:11
2015-04-14 16:49:11.503 test_bg10[1520:601603] 133.612857
2015-04-14 16:49:16.500 test_bg10[1520:601603] 16:49:16
2015-04-14 16:49:16.503 test_bg10[1520:601603] 128.612946
2015-04-14 16:49:21.501 test_bg10[1520:601603] 16:49:21
2015-04-14 16:49:21.503 test_bg10[1520:601603] 123.612186
2015-04-14 16:49:26.501 test_bg10[1520:601603] 16:49:26
2015-04-14 16:49:26.503 test_bg10[1520:601603] 118.612067
2015-04-14 16:49:31.501 test_bg10[1520:601603] 16:49:31
2015-04-14 16:49:31.503 test_bg10[1520:601603] 113.612460
2015-04-14 16:49:36.500 test_bg10[1520:601603] 16:49:36
2015-04-14 16:49:36.502 test_bg10[1520:601603] 108.613655
2015-04-14 16:49:41.497 test_bg10[1520:601603] 16:49:41
2015-04-14 16:49:41.499 test_bg10[1520:601603] 103.616916
2015-04-14 16:49:46.501 test_bg10[1520:601603] 16:49:46
2015-04-14 16:49:46.503 test_bg10[1520:601603] 98.612312
2015-04-14 16:49:51.501 test_bg10[1520:601603] 16:49:51
2015-04-14 16:49:51.503 test_bg10[1520:601603] 93.612255
2015-04-14 16:49:56.501 test_bg10[1520:601603] 16:49:56
2015-04-14 16:49:56.503 test_bg10[1520:601603] 88.612884
2015-04-14 16:50:01.501 test_bg10[1520:601603] 16:50:1
2015-04-14 16:50:01.503 test_bg10[1520:601603] 83.612934
2015-04-14 16:50:06.501 test_bg10[1520:601603] 16:50:6
2015-04-14 16:50:06.503 test_bg10[1520:601603] 78.612850
2015-04-14 16:50:11.501 test_bg10[1520:601603] 16:50:11
2015-04-14 16:50:11.503 test_bg10[1520:601603] 73.612921
2015-04-14 16:50:16.501 test_bg10[1520:601603] 16:50:16
2015-04-14 16:50:16.504 test_bg10[1520:601603] 68.611572
2015-04-14 16:50:21.501 test_bg10[1520:601603] 16:50:21
2015-04-14 16:50:21.503 test_bg10[1520:601603] 63.612793
2015-04-14 16:50:26.501 test_bg10[1520:601603] 16:50:26
2015-04-14 16:50:26.503 test_bg10[1520:601603] 58.612872
2015-04-14 16:50:31.501 test_bg10[1520:601603] 16:50:31
2015-04-14 16:50:31.504 test_bg10[1520:601603] 53.611517
2015-04-14 16:50:36.501 test_bg10[1520:601603] 16:50:36
2015-04-14 16:50:36.503 test_bg10[1520:601603] 48.612191
2015-04-14 16:50:41.501 test_bg10[1520:601603] 16:50:41
2015-04-14 16:50:41.503 test_bg10[1520:601603] 43.612224
2015-04-14 16:50:46.501 test_bg10[1520:601603] 16:50:46
2015-04-14 16:50:46.503 test_bg10[1520:601603] 38.612853
2015-04-14 16:50:51.501 test_bg10[1520:601603] 16:50:51
2015-04-14 16:50:51.503 test_bg10[1520:601603] 33.612277
2015-04-14 16:50:56.501 test_bg10[1520:601603] 16:50:56
2015-04-14 16:50:56.504 test_bg10[1520:601603] 28.612063
2015-04-14 16:51:01.500 test_bg10[1520:601603] 16:51:1
2015-04-14 16:51:01.502 test_bg10[1520:601603] 23.613674
2015-04-14 16:51:06.501 test_bg10[1520:601603] 16:51:6
2015-04-14 16:51:06.503 test_bg10[1520:601603] 18.612233
2015-04-14 16:51:11.501 test_bg10[1520:601603] 16:51:11
2015-04-14 16:51:11.504 test_bg10[1520:601603] 13.611825
2015-04-14 16:51:16.501 test_bg10[1520:601603] 16:51:16
2015-04-14 16:51:16.503 test_bg10[1520:601603] 8.612198


App執行沒多久,我就按HOME鍵,將App丟到背景去,可以看到
我建了一個計時器,每5秒就輸出現在時間以及背景可執行秒數。
結果輸出背景可執行時間一開始就只有178.多秒,然後每5秒又輸出時
真的背景可執行時間都約少了5秒。
直到只剩下8秒時,就不再輸出了,可以安全推論說,此時
App已經被suspend下來了。所以現在iOS進入背景後,可額外要求時間,
只有約3分鐘了?

留言

這個網誌中的熱門文章

D-BUS學習筆記

關於藍牙裝置找尋(inquiry, scan)兩三事

Cisco Switch學習筆記: EtherChannel