I have a UILocalNotification that is supposed to fire once a day, Monday through Friday, but not on the weekend. I thought that setting the repeatInterval property of the no
You have to set day separately for local notification and set local Notification repeatInterval to NSWeekCalendar Unit. AS
[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];
Here I have made the methods setDateForAlarmWithWeekday that returns date to be setted in fireDate property of local Notification and notificationWithItem that sets Local Notification.
-(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) {
localNotification.alertAction=@"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];
}
I hope this will help u.