如何在每个月的特定日期发出通知?

时间:2012-12-05 10:32:28

标签: objective-c nsdate

如何获得每个月的第5个日期。到目前为止,我知道如何获得当前日期。我只想在每个月的第5天发出本地通知。

这是我的代码:

 UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
 if (!localNotification){
         return; 
 }
 NSDate *date = [NSDate date];   
 NSDate *dateToFire = ?;  // please give me logic to find out 5th of evry month


 [localNotification setFireDate:dateToFire];
 [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];


 NSArray *array = [NSArray arrayWithObjects:@"Value 1", @"Value 2", nil];
 NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"];
 [localNotification setUserInfo:data];



 [localNotification setAlertBody:@"Incoming Local Notification" ];
 [localNotification setAlertAction:@"Open App"];
 [localNotification setHasAction:YES];      

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,那么你可以这样做:

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [dateComponents day];
if(day==5){
     //your logic starts or make some flag here.
}