gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:59:02 24 4
gpt4 key购买 nike

我有一个 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];

最佳答案

iOS 中的工作日仅表示一周中的一天。它没有与周末相对的“工作周”含义。

文档通过建议您使用 1-7 作为单位说得更清楚一些:

NSWeekdayCalendarUnit

Specifies the weekday unit.

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 设置参数的方法中。我已经向您展示了如何在星期一完成。

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;

关于ios - UILocalNotification 应该在每个工作日重复,但也会在周末触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9855147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com