0

私はそのようなものを1つの配列にしています:

_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時?
  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 に答える