0

My Mac App updates it's badge number at a certain time.
Here is my function:

- (void)setBadgeNumber
{
    // Sets the badge number to 1.
    [[[NSApplication sharedApplication] dockTile] setBadgeLabel:@"1"];

    // Sends a notification.
    NSUserNotificationCenter *nc = [NSUserNotificationCenter defaultUserNotificationCenter];
    NSUserNotification *notification = [[NSUserNotification alloc] init];
    notification.title = @"Update";
    notification.informativeText = @"There's something new in the app!";
    [nc deliverNotification:notification];
}

I was wondering if you can execute this function at a certain date (for example: 08-08-2015).
Even if the app isn't running (also if the app is running).
Does somebody know if you can execute a method at a certain date (and time)?

Developer
  • 406
  • 1
  • 4
  • 18
  • 2
    Mac OS X 10.8+: [Local Notifications](https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html) – vikingosegundo Jul 08 '15 at 17:46

4 Answers4

1

You've got quite a few options, depending on what you're after.

The obvious place to start is NSTimer.

There's also the performSelector withDelay function family.

Lastly, a cool extension I sometimes use, delayed blocks.

Oh, one more - you can also setup Local Notifications.

I prefer NSTimer's initWithFireDate:interval:target:selector:userInfo:repeats:

UPDATE

NSTimer:

NSTimer *saveProgressSizeTimer = [[NSTimer alloc]
    initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:2.0f]
    interval:1.0f
    target:self
    selector:@selector(myMethod:)
    userInfo:myUserInfo
    repeats:YES];  // This Will Be Executed After 2 Seconds

dispatch_after

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    NSLog(@"parameter1: %d parameter2: %f", parameter1, parameter2);
});  // This Will Be Executed After 10 Seconds
hmak.me
  • 3,770
  • 1
  • 20
  • 33
1

Launchd is usually associated with Launch Agents and Launch Daemons, but Apple also recommends this as a replacement for the Unix cron functionality.

Using this, you can create a launchd property list in order to schedule jobs to run periodically or on a specified calendar interval.

If you need your program to run code, use an argument that when called with the application, will call the appropriate function, then quit. Then, use launchd to call your program with that argument, at a specified date and time, by configuring the property list and adding it to the correct folder (e.g. /Library/LaunchAgents)

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Do you have an example? – Developer Jul 09 '15 at 14:09
  • Example of what? I've linked to how to create the property list and how to schedule the jobs to run at a calendar interval? If you mean do I have a working example to give you, then no. But once your application handles being [passed arguments](http://stackoverflow.com/questions/3115302/how-to-call-a-cocoa-app-from-command-line-outside) then you've got all the pieces you need. – TheDarkKnight Jul 09 '15 at 14:48
1

Declare a NSTimer property and call your method to check the is same or not.

 self.nstimer=  [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];

This will be your update method :

- (int)updateTime:(NSTimer *)timer
 {

NSString *Time1 = @"2016-02-20 03:19:05 +0000";
NSString *Time2 = @"2016-02-20 03:19:05 +0000";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"];

NSDate *tokonExpireDate1=[dateFormatter dateFromString:Time1];
NSDate *tokonExpireDate2=[dateFormatter dateFromString:Time2];


if ([tokonExpireDate1 compare:tokonExpireDate2] == NSOrderedDescending) {
    //"date1 is later than date2
    NSLog(@"Later Date");

} else if ([tokonExpireDate1 compare:tokonExpireDate2] == NSOrderedAscending) {
    //date1 is earlier than date2
    NSLog(@"Previous Date");

} else {
    //dates are the same

    NSLog(@"Same Date"); 
    //CALL your method here.
}
}

I called the method for 0.10 sec. If you need to call it by day interval ( 2days, 3 days later etc), then convert your day to second and call. Hope you know this. It will work. Thanks.

Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49
0

To execute a method at a specific time, have a look at NSTimer. However I don't know any way to execute a method of an application when the app is not currently running. As far as I know, there only exit ways to start the application at a specific time (with given parameters) or to use background stuff like for example a NSDockTilePlugin.

freytag
  • 4,769
  • 2
  • 27
  • 32