[英]Local notification if arrays date is same as today date
I have one array something like that: 我有一个像这样的数组:
_combinedBirthdates(
"03/05/2013",
"09/22/1986",
"03/02/1990",
"03/02",
"08/22/1989",
"11/02/1990",
"07/08",
"08/31/1990",
"05/13",
"07/11/2007",
"10/07/2010",
"02/20/1987")
i want Local notification if today date is same as date in above array 如果今天的日期与上面数组中的日期相同,我想要本地通知
I used following logic for notification: 我使用以下逻辑通知:
NSLog(@" _combinedBirthdates%@",_combinedBirthdates);
NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setDateFormat:@"MM/dd"];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDate *date1 =[NSDate date];
NSString *string =[Formatter1 stringFromDate:date1];
NSDate *todaydate =[Formatter1 dateFromString:string];
for (int i=0;i<_combinedBirthdates.count;i++)
{
NSDate *date =[Formatter1 dateFromString:[_combinedBirthdates objectAtIndex:i ]];
if(date == todaydate){
localNotif.fireDate = date;
localNotif.alertBody = @"birthdate notification";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Now my questions: 现在我的问题:
See the modified code.... 查看修改后的代码....
NSMutableArray *newBirthDates = [[NSMutableArray alloc] init];;
for(int i = 0; i < [_combinedBirthdates count]; i++)
{
NSString *date = [_combinedBirthdates objectAtIndex:i];
NSArray *dateComponents = [date componentsSeparatedByString:@"/"];
if([dateComponents count] == 3)
{
[newBirthDates addObject:[NSString stringWithFormat:@"%@/%@",[dateComponents objectAtIndex:0], [dateComponents objectAtIndex:1]]];
}
else
{
[newBirthDates addObject:date];
}
}
NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setDateFormat:@"MM/dd"];
NSDate *date1 =[NSDate date];
NSString *string =[Formatter1 stringFromDate:date1];
NSDate *todaydate =[Formatter1 dateFromString:string];
for (int i=0;i<newBirthDates.count;i++)
{
NSDate *date =[Formatter1 dateFromString:[newBirthDates objectAtIndex:i ]];
NSComparisonResult result = [date compare:todaydate];
if(result == NSOrderedSame)
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = date;
localNotif.alertBody = @"birthdate notification";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.