我正在创建一个游戏,当应用程序在后台运行或未运行时,用户可以获得积分奖励。如果应用程序完全关闭,他们仍应获得积分。目前我正在使用 NSTimer 执行此操作,但是我到处都读到计时器无法在后台执行。这是我所拥有的以及我应该如何修复它:
- (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"];
}
最佳答案
您可以在 applicationDidEnterBackground
方法中使用 NSUserDefault 设置“结束时间”。
在 applicationDidBecomeActive
中,您可以获得自“结束时间”以来耗时,并根据该耗时设置分数。
关于ios - 在后台执行游戏中的评分任务 Objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36134759/