2014-02-20 22 views
0

好吧,我没有得到有关这个问题的答案。 :(在后台执行间隔输出IOS 7

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; 
    }); 

} 

很明显,我IHAVE在同一范围内定义这个功能

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

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

这意味着不在背景上运行?

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

我真的很感谢一些帮助。

非常感谢。

回答

1

无论您的代码是否正常工作,除非您的应用程序中设置了UIBackgroundModes(VOIP,Location service,Audio ..),否则后台任务将在一段时间后(> 10分钟)终止。

关于后台执行检查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?

+0

好吧,我明白了,我读这一切后,并没有对我的作品,这不工作10分钟,这从来没有在后台运行,我已经启用VOIP,有更多的想法?谢谢 –

+0

好男人我只是想节省你的时间;),VOIP适合我,但要小心你的应用程序应该是VOIP或它被拒绝,这个解决方案应该为你工作http://stackoverflow.com/a/4808049/1262634 – null

+0

当您在启用VOIP背景模式下执行代码时?您在背景模式下接收输出? ..... –