Vlad
Vlad

Reputation: 96

Schedule a method one time a day Cocoa

Good Day!

I am developing a reminder software for Mac OS X (not iPhone). It should present a window one time in a day. The exact time is not defined, just one time. How can I make it. It register itself to start after the login. I have tried NSTimer but it looks like it doesn't fire a method when I call [timer setfFireDate:] after the first method fire.

Look forward for your help.

Upvotes: 0

Views: 1085

Answers (1)

rdelmar
rdelmar

Reputation: 104082

From your latest comments, it seems like you need to use both a timer and user defaults. The user defaults method first checks to see if there is a user default called "firstOpening" and if not sets it to the current time -- this will set the clock for the beginning of your 20 day trial period. That first if clause will only run the first time the program is opened.

I created a property, firstOpenTime, that retrieves the value from the user defaults, and uses that to check if the 20 days has expired -- if so, the program runs the presentExpiredWindow method, and if not, it presents the reminder window, and sets up a repeating timer that runs once every 24 hours. So, even if the program remains open all the time, the timer should fire once every 24 hours to show the reminder window (unless the 20 days has elapsed, and then the timer method calls the showExpiredWindow method and invalidates itself).

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"firstOpening"] ){
        [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"firstOpening"];
    }

    self.firstOpenTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"firstOpening"];
    if ([[NSDate date] timeIntervalSinceDate:self.firstOpenTime] > 1728000) { //1,728,000 seconds is 20 days
        [self presentExpiredWindow];
    }else{
        [self presentReminderWindow];
        [NSTimer scheduledTimerWithTimeInterval:86400 target:self selector:@selector(presentReminder:) userInfo:nil repeats:YES]; 
    }

}

-(void)presentReminder:(NSTimer *) aTimer {
    if ([[NSDate date] timeIntervalSinceDate:self.firstOpenTime] > 1728000) {
        [self presentExpiredWindow];
        [aTimer invalidate];
    }else{
        [self presentReminderWindow];
    }

}

-(void)presentReminderWindow {
    //show reminder window
}


-(void)presentExpiredWindow {
    //show trial period has ended window
}

Upvotes: 1

Related Questions