gpt4 book ai didi

ios - UILocalNotification 在整个星期的特定时间触发,除了 1 天

转载 作者:行者123 更新时间:2023-11-29 10:40:10 24 4
gpt4 key购买 nike

在我的应用程序中,我有一个 LocalNotification。我从 DatePicker 获取触发时间,我希望 LocalNotification 在特定时间(我从 UIPickerView 获取)和除星期六以外的所有工作日触发整个星期。

编辑

    UIDatePicker *picker = [[UIDatePicker alloc]init];
[picker setTag:kDatePickerTag];


for (int i = 0; i < 7; i++) {
NSDate *today = [NSDate date];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *scheduleDate = [**picker.date** dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
if (componentsForEachDay.weekday != 7) { // To skip Saturday

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSWeekCalendarUnit;
localNotification.fireDate = picker.date;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = @"test message";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

到目前为止,我有这段代码,但现在我必须设置触发时间,而且我不知道如何在给我时间的 picker.date 和给我日期的 weedDay 之间合并。我需要的是同一个 NSDate 对象。有什么想法吗?

最佳答案

如果您想跳过星期六,则必须为其他 6 天的每一天设置一个通知,并让它按周重复。

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
for (int i = 0; i < 7; i++) {
NSDate *scheduleDate = [firstScheduledDate dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
if (componentsForEachDay.weekday != 7) { // To skip Saturday

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSWeekCalendarUnit;
localNotification.fireDate = FIRE_DATE;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = YOUR_NOTIFICATION_MESSAGE;
localNotification.soundName = UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}

关于ios - UILocalNotification 在整个星期的特定时间触发,除了 1 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24952751/

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