0

我决定为我的第一个应用程序创建一个警报应用程序。到目前为止,我了解创建两个 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);
4

1 回答 1

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在警报触发后执行您的操作。

于 2013-10-24T21:46:09.720 回答