0

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

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

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

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

4

4 回答 4

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

试试这个你的问题会解决

于 2013-04-05T06:05:02.743 回答
0

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

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

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

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

于 2013-04-05T05:57:26.340 回答
0

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

- (void)applicationDidEnterBackground:(UIApplication *)application 
- (void)applicationWillEnterForeground:(UIApplication *)application 
于 2013-04-05T05:59:07.513 回答
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);
}
于 2013-04-05T06:11:37.277 回答