【发布时间】:2023-03-04 02:51:01
【问题描述】:
我正在尝试在后台运行一些 nstimer,实际上我使用此代码在模拟器上实现了它
- (void)start {
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) {
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
[application endBackgroundTask:background_task];
background_task = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
if (self.timer == nil) self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
}
}
}
问题是在真机上它不起作用,也许我应该启用一些后台模式?
【问题讨论】:
-
@Stela 是的,我找到了这个github.com/zhigang1992/ZGCountDownTimer,它工作得很好
标签: xcode nstimer background-process