在后台 IOS 7 上执行间隔输出



好的,我没有得到关于这个的答案。 :(

多对等连接音频流在后台停止工作

这个呢?

我正在尝试在后台运行此代码。

- (void)applicationDidEnterBackground:(UIApplication *)application {
    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
       [application endBackgroundTask:bgTask];
       bgTask = UIBackgroundTaskInvalid;
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSTimer *aTimer = [NSTimer timerWithTimeInterval:3
                                                  target:self
                                                selector:@selector(showInformation)
                                                userInfo:nil 
                                                 repeats:YES];
        [[NSRunLoop mainRunLoop]
           addTimer:aTimer
           forMode:NSDefaultRunLoopMode];
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

显然,我在同一范围内定义了这个函数

- (void) showInformation {
  NSLog(@"showInformation is called.");
}

但是当我将应用程序放在后台时,间隔消息,停止工作,当我回到前台时继续工作......

这意味着没有在后台运行?。

这可能吗?还是我试图做一些愚蠢的不可能的事情?

我真的很感激一些帮助。

多谢。

无论您的代码是否有效,您的后台任务都将在一段时间后(>10 分钟)被 iOS 终止,除非您在应用程序中设置了 UIBackgroundModes(VOIP、定位服务、音频......

有关后台执行

的详细信息,请查看后台执行和多任务处理。

iOS7 中的另一个选项是使用后台提取,但您无法控制时间(iOS 使用智能计时器)。

为了更好地理解,请查看Raywenderlich的iOS背景模式教程。

如果您需要一些工作,请查看以下SO帖子:

如何在我的 iOS 应用程序中每 n 分钟更新一次后台位置?

在后台运行iOS应用程序超过10分钟

如果用户强制退出我的应用,iOS 是否会在后台启动我的应用?

最新更新