甚至在应用程序之外不断比较两个NSDates

时间:2013-10-24 21:13:49

标签: objective-c nsdate alarm

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

[对客观c来说很新,对不起,谢谢你]

这就是我开始的:

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);

1 个答案:

答案 0 :(得分:1)

您不需要自己创建循环或比较日期。发出警报声的一种方法是通过本地通知。一个快速示例代码,用于安排自己的本地通知:

// 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执行操作。