问题描述
我有一个 UILocalNotification 应该每天触发一次,从周一到周五,但不是在周末.我认为将通知的 repeatInterval 属性设置为 NSWeekdayCalendarUnit 可以完成此操作.对我来说可悲的是,我的通知仍然在周末触发.谁能建议为什么?这是我的代码:
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 notification to NSWeekdayCalendarUnit would accomplish this. Sadly for me, my notifications are still firing on the weekend. Can anyone suggest why? Here is my code:
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];
推荐答案
iOS 中的工作日仅表示一周中的一天.它没有工作周";内涵而不是周末.
Weekday in iOS just means a day inside of a week. It doesn't have the "work week" connotation as opposed to a weekend.
文档说得更清楚一点,建议您使用 1-7 作为单位:
The documentation says it a little clearer, by suggesting that you use 1-7 as the units:
NSWeekdayCalendarUnit
NSWeekdayCalendarUnit
指定工作日单位.
对应的值为 kCFCalendarUnitSecond.等于 kCFCalendarUnitWeekday.工作日单位是从 1 到 N 的数字(对于公历 N=7,1 是星期日).
The corresponding value is an kCFCalendarUnitSecond. Equal to kCFCalendarUnitWeekday. The weekday units are the numbers 1 through N (where for the Gregorian calendar N=7 and 1 is Sunday).
来源:http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html
为了正确设置周一到周五的通知,这里有一些框架代码.你必须执行 5 次,所以最好将它封装在一个为 fireDate 提供参数的方法中.我已经向你展示了如何在星期一做到这一点.
To properly set your notifications from Monday through Friday, here's some skeleton code. Yo'll have to execute it 5 times, so it'd be good to encapsulate it inside of a method that parameters for the fireDate. I've shown how you could do it for Monday.
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;
这篇关于UILocalNotification 应该在每个工作日重复,但也会在周末触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)