2013-04-05 44 views
0

我是iOS編程的新手,我試圖製作一個非常簡單的應用程序,以瞭解有關生命週期和不同狀態的更多信息。該應用程序將在應用程序運行時存儲當前時間戳,並且每當它出現背景並返回時,它都會顯示一條消息,如「在xx分鐘後見到您很高興」。顯示應用程序在後臺的持續時間

現在,我的方法是這樣的,一開始會有一個時間戳存儲在變量中,當調用viewDidLoad()方法時,它將檢查時間戳與當前時間戳並顯示減去的值。

當應用程序進入後臺時,我將更改viewDidUnload()方法中本地時間戳變量的值,並且在返回時我將比較時間戳並顯示消息。

任何關於這樣做的正確方法的指針?

+0

我認爲,當應用程序進入後臺viewDidUnload()方法不會被調用。 – Balu 2013-04-05 05:57:27

回答

1

使用

把下面的方法

-(void)minCalculation_backgroundtime:(NSString *)backgroundTime forgroundTime:(NSString *)foreGroundTime 
{ 
    NSDateFormatter *dateformat = [[NSDateFormatter alloc]init]; 
    [dateformat setDateFormat:@"MM/dd/yyyy HH:mm:ss"]; 

    NSDate *lastDate = [dateformat dateFromString:foreGroundTime]; 
    NSDate *todaysDate = [dateformat dateFromString:backgroundTime]; 
    NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow]; 
    NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow]; 
    NSTimeInterval dateDiff = lastDiff - todaysDiff; 
    int min = dateDiff/60; 
    NSLog(@"Good to see you after %i minutes",min); 
} 

並替換爲以下兩種方法

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; 
    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"]; 
    NSString *backGroundTime = [dateFormat stringFromDate:[NSDate date]]; 
    [[NSUserDefaults standardUserDefaults]setValue:backGroundTime forKey:@"backGroundTime"]; 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; 
    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"]; 
    NSString *foreGroundTime = [dateFormat stringFromDate:[NSDate date]]; 
    NSString *backGroundTime = [[NSUserDefaults standardUserDefaults]valueForKey:@"backGroundTime"]; 
    [self minCalculation_backgroundtime:backGroundTime forgroundTime:foreGroundTime]; 
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
} 

試試這個您的問題將解決

0

您可以使用NSUserDefaults來保存上次的時間戳。

使用此功能時,您的應用程序在後臺準備

- (void)applicationWillResignActive:(UIApplication *)application 
- (void)applicationDidEnterBackground:(UIApplication *)application 
- (void)applicationWillTerminate:(UIApplication *)application 

當應用程序再次打開取出最後一次保存的時間戳與當前時間戳的差異。

0

在appdelegate.m文件中創建一個全局變量和viewControllers之間傳遞數據,並保持你的代碼在下面的方法..下面的代碼

- (void)applicationDidEnterBackground:(UIApplication *)application 
- (void)applicationWillEnterForeground:(UIApplication *)application 
+0

但是會有一個viewController,對吧?因此,我將創建一個名爲'helloMessage'的新方法,並使用適當的參數從這兩個函數中調用消息。這是做這件事的正確方法嗎? – Jyotiska 2013-04-05 06:08:48

+0

是的,你可以做到這一點沒問題。 – Balu 2013-04-05 06:11:36

0

你可以做這樣的事情:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterForeGround) name:UIApplicationDidEnterForegroundNotification object:nil]; 

-(void)didEnterBackground 
{ 
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults]; 
[userDefaults setObject:[[NSDate alloc]init] forKey:@"TimeStamp"]; 
[userDefaults synchronize]; 
} 

-(void)didEnterForeGround 
{ 
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults]; 
NSString *countString= [userDefaults objectForKey:@"TimeStamp"]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setTimeStyle:NSDateFormatterFullStyle]; 
NSTimeInterval *timeBG=[[dateFormatter dateFromString:countString] timeIntervalSinceDate:[NSDate alloc] init]]; 
NSLog(@"Time Background: %@",timeBG); 
} 
相關問題