ローカル通知のために1日を設定し、ローカル通知repeatIntervalをNSWeekCalendar Unitに設定する必要があります。
ここ
[self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:2 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
[self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:3 :tempDict]andRepeatInterval:NSWeekCalendarUnit];
[self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:4 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
[self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:5 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
[self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:6 :tempDict] andRepeatInterval:NSWeekCalendarUnit];
ように私は、ローカル通知を設定し、ローカル通知とnotificationWithItemのfireDateプロパティにsettedされる日付を返すメソッドsetDateForAlarmWithWeekdayを行いました。
-(void) notificationWithItem:(NSDictionary*)tmpdict Date:(NSDate *)date andRepeatInterval:(NSCalendarUnit)CalUnit
{
UILocalNotification *localNotification =[[UILocalNotification alloc]init];
if (localNotification==nil) {
return;
}
localNotification.fireDate=date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.repeatCalendar=[NSCalendar currentCalendar];
localNotification.alertBody=[NSString stringWithFormat:@"%@",[tmpdict objectForKey:@"Reminder"]];
NSDictionary *snoozeDic=[tmpdict objectForKey:@"Snooze"];
if ([[snoozeDic valueForKey:@"Switch"]intValue]==1) {
[email protected]"Snooze";
}else
{
localNotification.hasAction=NO;
}
localNotification.repeatInterval=CalUnit;
localNotification.soundName=[NSString stringWithFormat:@"%@.caf",[tmpdict objectForKey:@"Tone"]];
localNotification.userInfo=[NSDictionary dictionaryWithObject:tmpdict forKey:@"AlarmInfo"];
localNotification.applicationIconBadgeNumber=1;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
}
-(NSDate*)SetDateForAlarmWithWeekday:(int)WeekDay:(NSDictionary*)dics
{
NSLog(@"set date for alarm called");
NSCalendar *calendar=[NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone defaultTimeZone]];
unsigned currentFlag=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit;
NSDateComponents *comp=[calendar components:currentFlag fromDate:[NSDate date]];
NSArray *array=[[dics objectForKey:@"Time"] componentsSeparatedByString:@" "];
NSInteger hour=[[[[array objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
NSInteger min=[[[[array objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:1] intValue];
if ([[array objectAtIndex:1] isEqualToString:@"PM"]) {
hour=hour+12;
}
else
{
if (hour==12) {
hour=0;
}
}
comp.hour=hour;
comp.minute=min;
comp.second=0;
NSLog(@"set date for alarm (%i:%i:%i)",comp.hour,comp.minute,comp.second);
NSLog(@"weekday :%i ",WeekDay);
NSLog(@"comp weekday %i",comp.weekday);
int diff=(WeekDay-comp.weekday);
NSLog(@"difference :%i",diff);
int multiplier;
if (WeekDay==0) {
multiplier=0;
}else
{
multiplier=diff>0?diff:(diff==0?diff:diff+7);
}
NSLog(@"multiplier :%i",multiplier);
return [[calendar dateFromComponents:comp]dateByAddingTimeInterval:multiplier*secondsInOneDay];
}
私はこれが役に立つと願っています。
おかげで、それはそれを理解することが良いことです。 1週間に1回繰り返す5種類の通知を設定する唯一の方法はありますか? – Darren
はい。私はあなたが従うことができるいくつかのスケルトンコードで私の答えを更新しました。 – louielouie
大変感謝しています! 10種類の可能な毎日の通知があるので、これを実装するのは少し苦しいことですが。 – Darren