0
我正在创建一个游戏,其中用户在应用程序处于后台或未运行时获得奖励点数。如果申请完全封闭,他们仍应得分。目前我正在使用NSTimer进行此操作,但是我已经阅读了各处定时器无法在后台执行的信息。这里是我有什么,我应该如何解决这个问题:在后台执行游戏中的得分任务objective-c
- (void)applicationDidEnterBackground:(UIApplication *)application {
score = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"];
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(score) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void) score{
score++;
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"score"];
}
当您的应用程序处于后台时,允许后台任务运行的具体时间允许。当你通过那个时候,你不能再运行任何任务。您应该查看通知,而不是在后台运行任务。 –
我会考虑@TejaNandamuri – John55