皆さんこんにちは、アプリがバックグラウンドにあるときにメソッドを呼び出す必要があります。これは今私がこれを達成しようとしていますが、タイマーが呼び出されることはありません。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
self.timer = nil;
[self initTimer];
});
}
- (void)initTimer {
DebugLog(@"timer INIT!!!!!");
if (self.timer == nil) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3
target:self
selector:@selector(checkUpdates:)
userInfo:nil
repeats:YES];
}
}
- (void)checkUpdates:(NSTimer *)timer{
[UIApplication sharedApplication].applicationIconBadgeNumber++;
DebugLog(@"timer FIRED!!!!!");
[self initTimer];
}
checkUpdates
アプリがバックグラウンドにある間、N分または秒ごとにメソッドを呼び出す必要があります。アプリがバックグラウンドinitTimer
に入ると呼び出されますが、タイマーは呼び出されないことに注意してください。