Quote:
Originally Posted by franzwarning
Hello,
How could i make a button available only once a day?
Cheers,
George
|
Here is what I used to check it if it was over a day since the last visit:
Declare difference as an NSTimeInterval. This is my code that I used and it may not work for you but it should give you a push in the correct direction.
Code:
-(void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSTimeInterval date = [defaults doubleForKey:@"startTimeInterval"];
NSTimeInterval dateNow = [NSDate timeIntervalSinceReferenceDate];
difference = dateNow - date;
difference = difference / 84000;
if(difference >= 0 && difference <= 1) {
button.enabled = NO;
}
if(difference >= 1 ) {
dateNow = [NSDate timeIntervalSinceReferenceDate];
[defaults setDouble:dateNow forKey:@"startTimeInterval"];
[defaults synchronize];
button.enabled = YES;
}
}
It find the amount of seconds since the last time, then divides it by 84000 which is the amount of seconds in a day so you get a decimal number with the amount of days since. If it is greater than 1, it sets the current date, if it is less than 1 day, it doesn't re-set the current date. Again, this is untested as I ripped it from another app of mine, but you should get the picture I think.