Check if View is loaded from midnight

I have a ViewController that randomly shuffles an array and splashes out text in a Label (in a method viewDidLoad). The problem is that whenever I go to the same ViewController it shuffles again and I need to shuffle it once every day.

So I need to check if this ViewController was loaded earlier that day (i.e. since midnight), and then I can put the shuffle in the if statement. Can I schedule a shuffle at midnight, whether the application is open or not?

I studied setting boolean to NSUserDefaults: something like hasLoadedSinceMidnight, but then can't decide how to reset the boolean value at midnight.

+5
source share
2 answers

You can implement the AppDelegate significantTimeChange method:

-(void)applicationSignificantTimeChange:(UIApplication *)application {
    //tell your view to shuffle
}

This method is called every midnight and during significant time changes, such as changing the time zone. If your application is closed when an event is received, this method will be called when your application is open.

More information can be viewed here.

An additional way to do the same inside your ViewController, and not in AppDelegate, would be to add:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performAction) name:UIApplicationSignificantTimeChangeNotification object:nil];

and then you can just do the shuffle operation in the method of -(void)performAction;this ViewController.

+7
source

Instead of saving, BOOLyou can save the date / time ( NSDate) of the last shuffle.

, , viewDidAppear.

. NSTime : http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

NSDateFormatter : https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003643


UPDATE:

, . , , , . , , NSUserDefaults, . , , ( NSUserDefaults). ( , , , lastSavedDate currentDate.)

NSDate *currentDate = [[NSDate alloc] init];
NSDate *lastShuffleDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastShuffleDate"];

// check to see if there is a prior shuffle date
// if there is not, shuffle the array and save the current date
if (!lastShuffleDate) {
    NSLog(@"No object set for 'lastShuffleDate'");

    //[self shuffleMyArray];

    [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"lastShuffleDate"];
    return;
}

// set up the date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];

NSLog(@"Current Date: %@", [dateFormatter stringFromDate:currentDate]);
NSLog(@"Saved Date: %@", [dateFormatter stringFromDate:lastShuffleDate]);

// check to see if the dates are the same by comparing the dates as a string
if (![[dateFormatter stringFromDate:currentDate] isEqualToString:[dateFormatter stringFromDate:lastShuffleDate]]) {
    NSLog(@"Dates are different...!");

    //[self shuffleMyArray];

} else {
    NSLog(@"Dates are the same... (midnight has not passed)");
}

// save the time of the last shuffle
[[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"lastShuffleDate"];

, , .

// remote dateStyle and set timeStyle to check times
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"Current Time: %@", [dateFormatter stringFromDate:currentDate]);
NSLog(@"Saved Time: %@", [dateFormatter stringFromDate:lastShuffleDate]);
0

All Articles