// 初始化本地通知对象 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];
注:如果要实现周一、周二等同时重复提醒,需要添加多个以上通知来实现。