- (IBAction)start:(id)sender { [defaults setBool:YES forKey:@"notificationIsActive"]; [defaults synchronize]; self.message.text=@"Напоминание включено"; NSDate *date = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date]; components.calendar = calendar; components.hour = 7; components.minute = 37; date = components.date; if([date compare:[NSDate date]] < 0) { date = [date dateByAddingTimeInterval:60*60*24]; } UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.alertBody = @"ТЕКСТ НАПОМИНАНИЯ"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitDay;//NSCalendarUnitMinute; //Repeating instructions here. localNotification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; } 
  • 2
    so the question is what? How to convert 10 am to NSDate? - Max Mikheyenko
  • @MaxMikheyenko Mikheyenko, Yes, Everyday at 10 am - Muhammadnakshandi Omarov
  • and why all this code? - Max Mikheyenko

1 answer 1

Roughly speaking, find time like this (taking into account that the alarm clock can be set before 10 am or after)

 NSDate *date = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date]; components.calendar = calendar; components.hour = 10; components.minute = 0; date = components.date; if([date compare:[NSDate date]] < 0) { date = [date dateByAddingTimeInterval:60*60*24]; } 

to repeat it every day

 localNotification.repeatInterval = NSCalendarUnitDay;