0

我有一个类似的数组:

_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")

如果今天的日期与上述数组中的日期相同,我想要本地通知

我使用以下逻辑进行通知:

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];
    }

现在我的问题:

  1. 这段代码可以吗?
  2. 我需要设备来测试此代码还是可以在模拟器中测试它?
  3. 什么时候会出现通知?凌晨 12:00?
  4. 如果申请被关闭,会出现通知吗?
  5. 如果代码不正确,请修改它。
4

1 回答 1

2
  1. 你的代码有一些错误,我在这里更正了。
  2. 是的,我们可以在模拟器中运行它进行测试。
  3. UILocalNotifications 将在我们在相应通知中指定的日期触发。在分配通知时,我们将考虑提到的时间。如果我们没有设置时间,那么设备时间将被考虑为提到的触发日期。
  4. 即使应用程序也关闭了,本地通知也会触发,但在我们点击操作按钮之前它不会触发。看这个规范...

查看修改后的代码......

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];
    }
}
于 2013-03-14T13:49:55.587 回答