如何在特定日期执行方法?
How to execute a method at a certain date?
我的 Mac 应用会在特定时间更新其徽章编号。
这是我的功能:
- (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];
}
我想知道您是否可以在特定日期(例如:08-08-2015)执行此功能。
即使应用不是 运行(如果应用是 运行)。
有人知道您是否可以在特定日期(和时间)执行方法吗?
要在特定时间执行方法,请查看 NSTimer。但是,当应用程序当前不是 运行 时,我不知道有什么方法可以执行应用程序的方法。据我所知,只有退出方式可以在特定时间(使用给定参数)启动应用程序或使用后台内容,例如 NSDockTilePlugin。
根据您的需求,您有很多选择。
显而易见的起点是 NSTimer。
还有 performSelector withDelay 函数系列。
最后,我有时使用的一个很酷的扩展,延迟块。
哦,还有一个 - 您还可以设置本地通知。
我更喜欢NSTimer的initWithFireDate:interval:target:selector:userInfo:repeats:
更新
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
Launchd 通常与 Launch Agents 和 Launch Daemons 相关联,但 Apple 也建议将其作为 Unix cron 功能的替代品。
使用它,您可以create a launchd property list in order to schedule jobs to run periodically或在指定的日历间隔内。
如果您的程序需要 运行 代码,请使用一个参数,当应用程序调用该参数时,将调用适当的函数,然后退出。然后,通过配置 属性 列表并将其添加到正确的文件夹(例如 /Library/LaunchAgents[=18),使用 launchd 在指定的日期和时间使用该参数调用您的程序=])
声明一个 NSTimer 属性 并调用您的方法来检查是否相同。
self.nstimer= [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
这将是您的更新方法:
- (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.
}
}
我调用了该方法 0.10 秒。如果您需要按天间隔(2 天、3 天后等)调用它,则将您的日期转换为秒并调用。希望你知道这一点。它会起作用。谢谢。
我的 Mac 应用会在特定时间更新其徽章编号。
这是我的功能:
- (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];
}
我想知道您是否可以在特定日期(例如:08-08-2015)执行此功能。
即使应用不是 运行(如果应用是 运行)。
有人知道您是否可以在特定日期(和时间)执行方法吗?
要在特定时间执行方法,请查看 NSTimer。但是,当应用程序当前不是 运行 时,我不知道有什么方法可以执行应用程序的方法。据我所知,只有退出方式可以在特定时间(使用给定参数)启动应用程序或使用后台内容,例如 NSDockTilePlugin。
根据您的需求,您有很多选择。
显而易见的起点是 NSTimer。
还有 performSelector withDelay 函数系列。
最后,我有时使用的一个很酷的扩展,延迟块。
哦,还有一个 - 您还可以设置本地通知。
我更喜欢NSTimer的initWithFireDate:interval:target:selector:userInfo:repeats:
更新
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
Launchd 通常与 Launch Agents 和 Launch Daemons 相关联,但 Apple 也建议将其作为 Unix cron 功能的替代品。
使用它,您可以create a launchd property list in order to schedule jobs to run periodically或在指定的日历间隔内。
如果您的程序需要 运行 代码,请使用一个参数,当应用程序调用该参数时,将调用适当的函数,然后退出。然后,通过配置 属性 列表并将其添加到正确的文件夹(例如 /Library/LaunchAgents[=18),使用 launchd 在指定的日期和时间使用该参数调用您的程序=])
声明一个 NSTimer 属性 并调用您的方法来检查是否相同。
self.nstimer= [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
这将是您的更新方法:
- (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.
}
}
我调用了该方法 0.10 秒。如果您需要按天间隔(2 天、3 天后等)调用它,则将您的日期转换为秒并调用。希望你知道这一点。它会起作用。谢谢。