即使在应用程序外部也不断比较两个 NSDATE



我决定为我的第一个应用程序创建一个警报应用程序。到目前为止,我理解创建两个NSDate对象并将它们与isEqualToDate和UILocalNotifications进行比较以进行通知。如何不断将设置的日期与当前日期进行比较。我是否创建一个循环,或者是否有更有效的方法在目标 C 上?如何在后台持续检查?

[对目标C非常陌生,对不起,谢谢]

这是iv'e开始的:

NSDate * now = [NSDate date];
NSDate * later = [[NSDate alloc] initWithString:@"2001-03-24 10:45:32 +0600"];
NSComparisonResult result = [later compare:mile];
NSLog(@"%@", later);
NSLog(@"%@", mile);

您无需自己创建循环或比较日期。让闹钟响起的一种方法是通过本地通知。一个快速示例代码,用于安排自己的本地通知:

// Initialize your notification
NSUserNotification *notification = [[NSUserNotification alloc] init];
// Set the title of your notification
[notification setTitle:@"Alarm finished!"];
// Set the text of your notification
[notification setInformativeText:@"My Text"];
// Set the time and date on which the notification will be delivered (in this case 60 seconds after the current time)
[notification setDeliveryDate:[NSDate dateWithTimeInterval:60 sinceDate:[NSDate date]]];
// Set the sound, this can be either nil for no sound, NSUserNotificationDefaultSoundName for the default sound)
[notification setSoundName:NSUserNotificationDefaultSoundName];
// Schedule the notification
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];

如果您不想使用本地通知,但希望在警报完成后执行其他操作,则可以在警报触发后使用 NSTimer 执行操作。

最新更新