UILocalNotification 应该在每个工作日重复,但也会在周末触发

时间:2023-03-23
本文介绍了UILocalNotification 应该在每个工作日重复,但也会在周末触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 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 应该在每个工作日重复,但也会在周末触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Xcode 4.2:尝试使用越狱的 iPhone 3G 进行开发时出现错误 0xC002 下一篇:故事板 - UITabBarController

相关文章