-1

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

kAiN
  • 2,559
  • 1
  • 26
  • 54

2 Answers2

1

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];
  • 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 – kAiN Mar 21 '17 at 21:45
  • @iOSDev "in a few words what I look for is that the increase uiprogressview must be obtained from the clock iphone minutes" Yes, and so you should read this answer carefully. It's telling you in great detail exactly how to do this. – matt Mar 21 '17 at 21:49
  • The only way I missed a comment ... I wanted to edit my message @matt ... – kAiN Mar 21 '17 at 21:52
  • I'm not sure, but i think you should use NSTimer to refresh the state of your progress in each unit of time, even if you have your ViewController with ProgressView is active. Am I right? – Jakub Tudruj Mar 21 '17 at 21:56
  • the method indicated by you is right, it works perfectly if I am applying the problem is when I close my app. Let me explain .. after 10 minutes my view progress increases the exact value but when I close my app and reopen the calculation starts from scratch and this is wrong because it should continue to calculate the time even though my app is closed ... perhaps my mistake to insert code somewhere? – kAiN Mar 22 '17 at 08:52
0

Upon app launch or whenever you want the progress to start, store current time:

let startTime = NSDate()

Then on refresh (can through timer setup to fire every few minutes, or in viewDidAppear()):

let timeDelta = Date().timeIntervalSince(startTime)
progressBar.progress =  timeDelta/(60 * 5) * 0.1

Update: Sounds like OP needs to know how to persist the progress start time between app termination and fresh launch. Like the other answer you could use the UserDefaults and check if a start time already exists. If it exists then use it otherwise create a new entry like so:

let userDefaultKey = "startTime"
var startTime = UserDefaults.standard.object(forKey: userDefaultKey)
if (startTime == nil)
{
   startTime = Date()
   UserDefaults.standard.set(startTime, forKey: userDefaultKey)
}
Garfield81
  • 521
  • 2
  • 9