Search code examples
iosobjective-cnsdatecgfloat

Subtract 1 minute between two NSDate


in my app I have a UIProgressView that shows the time elapsed between two NSDate. Everything works properly, each minute that passes UIProgressView advances by 0.1 per minute.

The difference between the two dates is calculated in this way with a CGFloat

Besides that I have a UIButton it should have the function of "decrease the ProgressView 0.1" in a few words each time the button is pushed I would like to take one minute to the difference between the two NSDate that I created previously. I made a few attempts but I can not get this because when my app is closed and then reopened the time difference between the two dates do not change ... how can I do this?

I show you the code I'm using

-(void)viewDidLoad {
    [super viewDidLoad];
    [self startDate];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self upgradeEnergyBar];
    [NSTimer scheduledTimerWithTimeInterval: (1 * 60)
                                     target:self
                                   selector:@selector(upgradeEnergyBar)
                                   userInfo:nil
                                    repeats:YES];
}

-(void)startDate {
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    if (! [defaults boolForKey:@"notFirstRun"]) {
        NSDate *date = [NSDate new];
        [[NSUserDefaults standardUserDefaults] setObject:date forKey:@"open"];
        NSLog(@"Ora di apertura : %@", date);
        [defaults setBool:YES forKey:@"notFirstRun"];
    }
}

- (IBAction)sfAction:(id)sender {
    //Decrement CGFLOAT
}

-(void)upgradeEnergyBar {
    self.now = [NSDate new];
    NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"open"];
    NSTimeInterval timeInterval = [self.now timeIntervalSinceDate:lastDate];
    _minutes = timeInterval / 60;

    _energyBar.progress = _minutes * 0.1;
    [self updateLabelEnergyWithProgress:_energyBar.progress];
    _minute.text = [NSString stringWithFormat:@"MINUTES:%ld", (long) _minutes];


    NSLog(@"MINUTES:%ld", (long) _minutes);

    if (_energyBar.progress == 1) NSLog(@"end");
}

Solution

  • You seem to have a couple questions in there, but to answer your "how to Subtract a minute" ...

    What you really want to do is ADD a minute to your saved date. So, on sfAction: you could:

    - (IBAction)sfAction:(id)sender {
    
        // get the 'saved' date/time
        NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"open"];
    
        // add one minute (60 seconds)
        lastDate = [lastDate dateByAddingTimeInterval:60];
    
        // get the current date/time
        NSDate *now = [NSDate date];
    
        // use the earlier date/time (to avoid a date/time in the future)
        NSDate *earlier = [lastDate earlierDate:now];
    
        // save it back to User Defaults
        [[NSUserDefaults standardUserDefaults] setObject:earlier forKey:@"open"];
    
    }
    

    Edit: This now includes code to make sure we're not incrementing the saved date/time to be later than the current date/time.

    Side note...

    It's really not a good idea to constantly read/write User Defaults.

    You should read the values when your app launches (or perhaps also when it returns to the foreground) and save the values in variables / properties.

    When your app is exiting (or being sent into the background) you can write the updated values.