您不需要自己创建循环或比较日期。让闹钟响起的一种方法是通过本地通知。为自己安排本地通知的快速示例代码:
// 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
在警报触发后执行您的操作。