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