我确定这个问题在某处重复了,但我找不到解决方案。我正在制作一款应用,其中一项功能允许用户选择他们将收到本地通知的日期和时间。
他们可以选择他们喜欢的一天中的任何时间,并且可以切换一周中的不同日期(周一、周二、周三等)。通知将每周发送一次。因此,我将用户限制为仅创建 3 个通知 - 如果选择了所有 7 天,我会将 repeatInterval
设置为每天(一个通知)。如果为每 3 个通知选择 6 天,那么我每天都需要一个单独的通知(总共 3x6=18 个通知)。很可能只会使用 1 个通知,所以这很好。
我知道如何设置 future 某个时间的通知,但我该如何设置周一下午 6 点的通知?
下面是我用来测试的代码。它在未来设置了 4 秒的警报(我是从 applicationDidEnterBackground
调用它的)。
NSDateComponents *changeComponent = [[NSDateComponents alloc] init];
changeComponent.second = 4;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *itemDate = [theCalendar dateByAddingComponents:changeComponent toDate:[NSDate date] options:0];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval = NSWeekdayCalendarUnit;
最佳答案
与您现在的做法完全相同,但创建日期的方式不同:
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:1];
[comps setMonth:1];
[comps setYear:2013];
[comps setHour:10];
[comps setMinute:10];
[comps setSecond:10];
localNotif.fireDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
关于iOS 在特定日期设置本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19276361/