我正在实现一个提醒应用程序。有两个提醒(一个用于每天,另一个用于 future 日期。)每天的提醒工作正常。在应用程序中有一些提示,当用户喜欢一个提示时,我希望它能够为将来的某个日期保存该提示(例如:8 月 29 日晚上 9 点)。谁能帮我实现这个场景的正确路径是什么。所有提示都保存在一个数组中。我如何保存不同的时间和日期用户从日期选择器中选择一个数组并在正确的时间触发本地通知并显示当天保存的提示用户。 (我尝试使用 NSUserdefaults 来保存日期和时间,还从数组中检索提示的索引并比较两者但不起作用)。
最佳答案
如上所述,您可以通过 UILocalNotification
执行此操作。听起来您在将来安排通知时遇到问题?试试下面的代码片段:
// get app instance
UIApplication *app = [UIApplication sharedApplication];
// create local notif
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
if (notification) {
NSDate *oneMinuteFromNow = [[NSDate date] dateByAddingTimeInterval:60];
notification.fireDate = oneMinuteFromNow;
notification.timeZone = [NSTimeZone defaultTimeZone];
NSString *notificationMessage = @"Alarm";
notification.alertBody = notificationMessage;
notification.soundName = UILocalNotificationDefaultSoundName;
// schedule notification
[app scheduleLocalNotification:notification];
// fire notification right away
[app presentLocalNotificationNow:notification];
}
可以更新变量 "oneMinuteFromNow"
以反射(reflect)您需要的 future 约会。仅供引用,如果您需要重复通知,Apple 为您提供的重复间隔有一些限制(例如,它们只能重复每个 NSSecondCalendarUnit、NSMinuteCalendarUnit、NSHourCalendarUnit、NSWeekdayCalendarUnit
等)。
关于iphone - 在 iphone 中设置本地通知(每天一个,另一个用于 future 日期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7229898/