Search code examples
iosobjective-cxcodensdateuiprogressview

Update UIProgressView each 5 Minutes from current Time


in my app I have a UIProgressView that needs increase by +0.1 every 5 minutes. The method for doing this, but I know I have a problem, because I would like the calculations progressview five minutes even if my application is then closed should rely on real-time (iphone clock) and not that when my application is open ... I'll explain

  1. I open my app and begin counting the minutes
  2. Then I close my app
  3. I open my app after 10 minutes
  4. my view progress should have a value of +0.2 (0.1 every 5 minutes)

I know that to do that I should use NSDate but can not implement it in the right way, could someone help me with an example to better understand how to implement this?

Thank you all

in a few words what I look for is that the increase uiprogressview must be obtained from the clock iphone minutes ... this has to happen is if the app is open or closed .... just one more minute comes uiprogressview must change. .. in short, this should be a recover energy from a user


Solution

  • I think you should:

    1. Store start date in some variable: NSDate *startDate = [NSDate new];

    save actual start date to NSUserDefaults:

    [[NSUserDefaults standardDefaults] setObject: startDate forKey: @"MyKey"];

    1. Next, when your application will became active get the date from NSUserDefaults, calculate time interval between saved date and actual date:

    NSTimeInterval secondsBetween = [[NSDate new] timeIntervalSinceDate: savedDate];

    1. Now you can divide result by your progress interval (I guess in ViewWillAppear):

    progress = secondsBetween/(5*60) * 0.1

    If you want to refresh your progress after each time unit when your ViewController is active you should use NSTimer:

    [NSTimer scheduledTimerWithTimeInterval: (5 * 60)
        target:self
        selector:@selector(refreshProgressMethod)
        userInfo:nil
        repeats:YES];