問題描述
我有一個(gè) UILocalNotification 應(yīng)該每天觸發(fā)一次,從周一到周五,但不是在周末.我認(rèn)為將通知的 repeatInterval 屬性設(shè)置為 NSWeekdayCalendarUnit 可以完成此操作.對我來說可悲的是,我的通知仍然在周末觸發(fā).誰能建議為什么?這是我的代碼:
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 中的工作日僅表示一周中的一天.它沒有工作周";內(nèi)涵而不是周末.
Weekday in iOS just means a day inside of a week. It doesn't have the "work week" connotation as opposed to a weekend.
文檔說得更清楚一點(diǎn),建議您使用 1-7 作為單位:
The documentation says it a little clearer, by suggesting that you use 1-7 as the units:
NSWeekdayCalendarUnit
NSWeekdayCalendarUnit
指定工作日單位.
對應(yīng)的值為 kCFCalendarUnitSecond.等于 kCFCalendarUnitWeekday.工作日單位是從 1 到 N 的數(shù)字(對于公歷 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
為了正確設(shè)置周一到周五的通知,這里有一些框架代碼.你必須執(zhí)行 5 次,所以最好將它封裝在一個(gè)為 fireDate 提供參數(shù)的方法中.我已經(jīng)向你展示了如何在星期一做到這一點(diǎn).
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;
這篇關(guān)于UILocalNotification 應(yīng)該在每個(gè)工作日重復(fù),但也會在周末觸發(fā)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!