2016-03-21 40 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