iOS程序压入后台,延续运行十分钟的方法

来源:互联网 发布:编程专用笔记本 编辑:程序博客网 时间:2024/06/01 07:13

模拟器测试不准确,需要真机测试

UIBackgroundTaskIdentifier _backgroundTask;- (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.    [self beginBackgroundTask];    int count = 0;    while (YES) {        NSLog(@"%d",count);        count ++;        [NSThread sleepForTimeInterval:5];    }    [self endBackgroundTask];}- (void)beginBackgroundTask{    _backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{        [self endBackgroundTask];    }];}- (void)endBackgroundTask{    [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];    _backgroundTask = UIBackgroundTaskInvalid;}


0 0
原创粉丝点击