显示应用在后台的持续时间

时间:2013-04-05 05:50:08

标签: iphone ios xcode cocoa-touch

我是iOS编程的新手,我正在尝试创建一个非常简单的应用程序,以了解有关生命周期和不同状态的更多信息。该应用程序将存储应用程序运行时的当前时间戳,每当它运行后台并重新启动时,它将显示“很高兴在xx分钟后看到你”的消息。

现在,我的方法是这样的,在开始时会有一个存储在变量中的时间戳,并且当调用viewDidLoad()方法时,它将使用当前时间戳检查时间戳并显示减去的值。

当应用程序进入后台时,我将在viewDidUnload()方法中更改本地时间戳变量的值,当重新启动时,我将比较时间戳并显示消息。

有关正确做法的指示?

4 个答案:

答案 0 :(得分:1)

在appdelegate.m文件中使用以下代码

下面的方法

-(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.
}

试试这个你的问题会解决

答案 1 :(得分:0)

您可以使用NSUserDefaults保存上次时间戳。

当您的应用在后台进行时使用此功能

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

当app再次打开时,取出上次保存的时间戳与当前时间戳的差异。

答案 2 :(得分:0)

创建一个全局变量并在viewControllers之间传递数据,并将代码保存在下面的方法中。

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

答案 3 :(得分: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);
}