在特定时间从现在开始在第二个星期二安排本地通知

时间:2016-01-04 08:36:35

标签: ios objective-c iphone

我必须从现在到第二个星期二,第三个星期五安排本地通知等。所以请说明我如何获得FireDate。任何帮助将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

以下是如何使用NSDateComponentsNSCalendar在下周的周二。我希望这有帮助

NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekday|NSCalendarUnitWeekOfYear fromDate:now];

    components.weekday = 3; //Tuesday on Gregorian calendar
    components.weekOfYear += 1; //next week
    NSDate *endDate = [calendar dateFromComponents:components];

endDate这里是您本地通知的fireDate。有关此问题的进一步帮助,请阅读NSDateComponents文档。祝你好运!

答案 1 :(得分:0)

 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *c = [[NSDateComponents alloc]init];


[c setYear:2012];
[c setWeekday:3]; //1 for Sunday 2 for monday 3 for Tuesday
[c setWeekdayOrdinal:2]; // The 2rd Tuesday

for (int i = 1; i <= 12; i++) {
     [c setMonth:i];
NSDate *date = [gregorian dateFromComponents:c];
NSLog(@"Date[%i]: %@",i,date);  
}

从此您将获得每个月的星期二日期。然后,您可以在1小时后显示通知时间的本地通知,因此请使用

 NSDate *firedate = [NSDate dateWithTimeInterval:1*60*60 sinceDate:tuesdayDate];