2013-10-31 10 views
5

毎日午前8時に30日間連続してUILocalNotificaionをスケジュールしたいので、その機能を1つのUILocationNotificationインスタンスでのみ実装したいと考えています。ローカル通知をスケジュールするコードは次のとおりです。30日間連続してUILocalNotificationを設定する方法

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"HH:mm"]; 
NSDate *date = [[NSDate alloc] init]; 
date = [formatter dateFromString:@"08:00"]; 
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = date; 
localNotification.timeZone=[NSTimeZone defaultTimeZone]; 
localNotification.alertBody = @"You just received a local notification"; 
localNotification.alertAction = @"View Details"; 
localNotification.soundName = UILocalNotificationDefaultSoundName; 
localNotification.repeatInterval = NSDayCalendarUnit; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
[formatter release]; 
[date release]; 

午前8時に毎日通知が発生しますが、30日間連続して通知されます。一番簡単な解決策は、30のUILocalNotificationsを起動することですが、私は1つのUILocalNotificationインスタンスでそれを行いたいと思います。どうやってやるの?助けてください。

答えて

4

UILocalNotificationでは、userInfoプロパティは、通知を作成するときにNSDateオブジェクトを保存します。 通知を開くと、 は、現在の日付をuserinfoでチェックします。 30日以上の相違がある場合は、ローカル通知をキャンセルすることができます。

+2

あなたのソリューションが動作します。しかし、ユーザーが通知を受け取ったり確認したりしないとどうなるでしょうか? –

+0

@ShahidIqbalがこの特定の質問に対する答えを見つけましたか? – Ilario

+0

@llario私はカレームの答えに行かなければならないと思う。 –

0
NSDate *now = [NSDate date]; 

// now a NSDate object for now + 30 days 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; 
[offsetComponents setDay:30]; 
NSDate *endDate = [gregorian dateByAddingComponents:offsetComponents toDate:now options:0]; 

ていることを確認した後:ユーザーの確認や通知を受信した場合

if([currentDate isEarlierDate:endDate]){ 
//Fire the notification 
}