2016-03-21 41 views
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"]; 
} 
+0

当您的应用程序处于后台时,允许后台任务运行的具体时间允许。当你通过那个时候,你不能再运行任何任务。您应该查看通知,而不是在后台运行任务。 –

+0

我会考虑@TejaNandamuri – John55

回答

1

您可以设置“结束时间”与NSUserDefault在applicationDidEnterBackground方法。 在applicationDidBecomeActive中,您会看到自“结束时间”以来经过的时间,并根据此过去的时间设置您的分数。

+0

好吧,但是我怎样才能从结束时间 – John55