Dar*_*ren 10 ios uilocalnotification ios5
我有一个UILocalNotification应该每周一次,周一到周五开火,但不是在周末.我认为将通知的repeatInterval属性设置为NSWeekdayCalendarUnit可以实现此目的.可悲的是,我的通知仍在周末开火.谁有人建议为什么?这是我的代码:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertAction = @"View";
localNotification.alertBody = NSLocalizedString(@"ALERT_MESSAGE", nil);
localNotification.soundName = UILocalNotificationDefaultSoundName;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];
// Notification fire times are set by creating a notification whose fire date
// is an arbitrary weekday at the correct time, and having it repeat every weekday
NSDate *fireDate = [dateFormatter dateFromString:@"01-04-2012 11:00"];
localNotification.fireDate = fireDate;
localNotification.repeatInterval = NSWeekdayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
break;
[localNotification release];
Run Code Online (Sandbox Code Playgroud)
lou*_*uie 15
iOS中的工作日只意味着一周内的一天.与周末相比,它没有"工作周"的内涵.
通过建议您使用1-7作为单位,文档说它更清楚一点:
NSWeekdayCalendarUnit
指定工作日单位.
对应的值是kCFCalendarUnitSecond.等于kCFCalendarUnitWeekday.工作日单位是数字1到N(其中公历N = 7,1表示星期日).
资料来源:http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html
要从周一到周五正确设置通知,这里有一些框架代码.你将不得不执行5次,所以将它封装在fireDate参数的方法中是很好的.我已经展示了如何在星期一这样做.
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
// Set this to an NSDate that is for the time you want, on Monday
notification.fireDate = fireDate;
// Repeat every week
notification.repeatInterval = NSWeekCalendarUnit;
Run Code Online (Sandbox Code Playgroud)
您必须分别为本地通知设置日期,并将本地通知repeatInterval设置为NSWeekCalendar单元.如
[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];
Run Code Online (Sandbox Code Playgroud)
这里我创建了方法setDateForAlarmWithWeekday,它返回在本地Notification的fireDate属性和设置Local Notification的notificationWithItem中设置的日期.
-(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];
}
Run Code Online (Sandbox Code Playgroud)
我希望这会对你有所帮助.
| 归档时间: |
|
| 查看次数: |
9405 次 |
| 最近记录: |