【问题标题】:Displaying the time duration the app was in background显示应用在后台的持续时间
【发布时间】:2013-04-05 05:50:08
【问题描述】:

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

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

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

关于这样做的正确方法的任何指针?

【问题讨论】:

  • 我认为当应用进入后台时 viewDidUnload() 方法没有被调用。

标签: iphone ios xcode cocoa-touch


【解决方案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.
}

试试这个你的问题会解决的

【讨论】:

    【解决方案2】:

    您可以使用 NSUserDefaults 保存最后一个时间戳。

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

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

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

    【讨论】:

      【解决方案3】:

      创建一个全局变量并在视图控制器之间传递数据并将您的代码保存在以下方法中......

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

      【讨论】:

      • 但是会有一个viewController,对吧?因此,我将创建一个名为“helloMessage”的新方法,并使用适当的参数从这两个函数调用消息。这是正确的做法吗?
      【解决方案4】:

      你可以这样做:

      [[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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多