iOS本地通知UILocalNotification--重复提醒(周一、周二、周三...)

本文介绍如何在iOS应用中设置重复的本地通知,包括指定星期、时间和内容等关键参数,确保应用程序能在特定周期内提醒用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 初始化本地通知对象  newWeekDay为需要重复的星期

     UILocalNotification *notification = [[UILocalNotification alloc] init];

                

     notification.timeZone = [NSTimeZone defaultTimeZone]; // 使用本地时区

                

     NSDateComponents * components = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:[NSDate date]];

                

     NSLog(@"设置的weekday = %d", newWeekDay);

            

     int hour = [[timeArr firstObject] intValue];

                

     int minute = [[timeArr lastObject] intValue];

                    

     NSDateComponents *comps = [[NSDateComponents alloc] init] ;


     NSInteger unitFlags = NSEraCalendarUnit |

                NSYearCalendarUnit |

                NSMonthCalendarUnit |

                NSDayCalendarUnit |

                NSHourCalendarUnit |

                NSMinuteCalendarUnit |

                NSSecondCalendarUnit |

                NSWeekCalendarUnit |

                NSWeekdayCalendarUnit |

                NSWeekdayOrdinalCalendarUnit |

                NSQuarterCalendarUnit;

                

     comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:[NSDate date]];

     [comps setHour:hour];

     [comps setMinute:minute];

     [comps setSecond:0];

                

     int temp = 0;

     int days = 0;

                

     temp = newWeekDay - components.weekday;

     days = (temp >= 0 ? temp : temp + 7);

     NSDate *newFireDate = [[[NSCalendar currentCalendar] dateFromComponents:comps] dateByAddingTimeInterval:3600 * 24 * days];

                

     notification.fireDate = newFireDate;

                

     // 设置重复间隔

     notification.repeatInterval = kCFCalendarUnitWeek;

                

     NSLog(@"具体启动的时间:%@", notification.fireDate.description);

                

     // 设置提醒的文字内容

     notification.alertBody   = @"亲,该称量体重了!";

                

     // 通知提示音 使用默认的

     notification.soundName= UILocalNotificationDefaultSoundName;

                

     NSDictionary *dic = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@%@", @"zhouleizhao", [AppDelegate getInstance].currentLoginID] forKey:@"key"];

                

     notification.userInfo = dic;

                

     // 将通知添加到系统中

     [[UIApplication sharedApplication] scheduleLocalNotification:notification];


     注:如果要实现周一、周二等同时重复提醒,需要添加多个以上通知来实现。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值