2013-03-23 19 views
0

我試圖找出應用程序在後臺的時間量。一切都編譯好,但我的計數器並沒有增加應用程序在後臺的時間量。計算應用程序在後臺的秒數

-(void)applicationWillResignActive:(UIApplication *)application { 

    closeTime = [NSDate date]; 

    NSUserDefaults *defaultsCloseTime; 
    [defaultsCloseTime setInteger:closeTime forKey:@"closeTimeKey"]; 
    [defaultsCloseTime synchronize]; 

} 

-(void)applicationWillEnterForeground:(UIApplication *)application { 

    closeTime = [[NSUserDefaults standardUserDefaults] integerForKey:@"closeTimeKey"]; 

    timeInterval = [closeTime timeIntervalSinceNow] * -100; 

} 


Counter 

在我的視圖控制器:

counterInt = time + counterInt; 
+0

加上'的NSLog(@ 「應用程序沒有主動辭職」);',以確保該方法獲取調用。 – 2013-03-26 13:57:11

回答

0

你應該synchronize寫關閉時間到用戶默認設置後。同樣在你的問題中,你在哪裏閱讀counterInt並且你還將它存儲爲用戶默認值?

一件事:

NSInterger time = round(timeInterval); 
+0

謝謝!完全錯過了同步。這仍然沒有工作。 timeInterval我根本沒有任何價值 – 2013-03-26 01:28:48