【问题标题】:Executing Interval output on background IOS 7在后台 IOS 7 上执行间隔输出
【发布时间】:2014-02-20 03:17:09
【问题描述】:

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

Multipeer Connectivity audio streaming stop work on background

这个呢?

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

- (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.");
}

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

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

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

非常感谢一些帮助。

非常感谢。

【问题讨论】:

    标签: ios objective-c ios7 nstimer multipeer-connectivity


    【解决方案1】:

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

    有关后台执行的更多信息,请查看Background Execution and Multitasking

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

    为了更好地理解,请查看 Raywenderlich 的Background Modes in iOS Tutorial

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

    How do I get a background location update every n minutes in my iOS application?

    Running iOS App in background for more than 10 minutes

    Will iOS launch my app into the background if it was force-quit by the user?

    【讨论】:

    • 好的,我明白了,我正在阅读所有这篇文章,但对我不起作用,这 10 分钟不起作用,这永远不会在后台运行,我启用了 VOIP,还有其他想法吗?谢谢
    • 好吧,我只是想节省您的时间 ;),VOIP 对我来说很好用,但请注意您的应用应该是 VOIP 否则会被拒绝,这个解决方案应该适合您stackoverflow.com/a/4808049/1262634跨度>
    • 在启用 VOIP 后台模式的情况下执行代码时?您在后台模式下收到输出? .....
    • 问题中的代码每 3 秒打印一次,但是当您将应用程序置于后台时停止打印。问题是。是否有某种方法可以在一段时间内保持在后台模式下打印输出?
    猜你喜欢
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多