好吧,我不知道这件事的答案。:(
那这个呢?
我试着在后台运行这段代码。
- (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.");
}但是当我把应用程序放在后台,间隔消息,停止工作,当我回到前台继续工作.
这意味着不是在后台运行?
这个是可能的吗?还是我想做些愚蠢的不可能的事?
我真的很感谢你的帮助。
非常感谢。
发布于 2014-02-20 07:43:52
无论您的代码是否有效,您的后台任务将在一段时间后(>10分钟)由iOS终止,除非在您的应用程序中设置了UIBackgroundModes (VOIP、位置服务、音频.)。
有关背景执行检查的更多信息,请检查后台执行和多任务处理。
iOS7中的另一个选项是使用背景提取,但您无法控制时间(iOS使用的是智能计时器)。
要更好地理解,请查看Raywenderlich的iOS教程中的后台模式。
如果你需要一些有用的东西,请检查下面的帖子:
https://stackoverflow.com/questions/21897377
复制相似问题