It is necessary to send reminders once a day, at a set time. I set the time in the format from 0 to 23 hours when using UIStepper and UISwitch. Two identical notifications come at the same time, I checked with breakpoint, but setEveryDayNotification did not find any repetitions ...

var hour = 0.0 var remindOnOff = false let notification = UILocalNotification() @IBOutlet weak var remindStepper: UIStepper! @IBOutlet weak var remindLabel: UILabel! @IBOutlet weak var remindSwitch: UISwitch! 

In viewDidLoad ()

  remindStepper.wraps = true remindStepper.value = hour remindStepper.minimumValue = 0 remindStepper.maximumValue = 23 //ЧТЕНИЕ УСТАНОВЛЕННОГО ВРЕМЕНИ УВЕДОМЛЕНИЯ if NSUserDefaults.standardUserDefaults().objectForKey("hour") != nil { hour = NSUserDefaults.standardUserDefaults().objectForKey("hour") as! Double } if NSUserDefaults.standardUserDefaults().objectForKey("remindOnOff") != nil { remindOnOff = NSUserDefaults.standardUserDefaults().objectForKey("remindOnOff") as! Bool } if remindOnOff == true { remindSwitch.on = true } else { remindSwitch.on = false } remindLabel.text = "\(Int(hour)):00" 

Create a notification

  func setEveryDayNotification(date:NSDate){ for index in 0...365 { let notification: UILocalNotification = UILocalNotification() notification.alertTitle = "GuyFit" notification.alertBody = NSLocalizedString("Reminder of the workout", comment:"") notification.soundName = UILocalNotificationDefaultSoundName notification.alertAction = NSLocalizedString("Go to GuyFit", comment:"") notification.timeZone = NSTimeZone.defaultTimeZone() notification.fireDate = date.dateByAddingTimeInterval(Double(index*60*60*24)) notification.repeatInterval = NSCalendarUnit.Day UIApplication.sharedApplication().scheduleLocalNotification(notification) } } 

Set the time value

 @IBAction func remindStepper(sender: UIStepper) { NSUserDefaults.standardUserDefaults().setObject(Int(sender.value), forKey: "hour") NSUserDefaults.standardUserDefaults().synchronize() remindLabel.text = "\(String(format: "%.0f", arguments: [sender.value])):00" UIApplication.sharedApplication().cancelAllLocalNotifications() remindOnOff = false remindSwitch.on = false NSUserDefaults.standardUserDefaults().setBool(remindOnOff, forKey: "remindOnOff") } 

Turn on and off notifications

  @IBAction func remindSwitch(sender: UISwitch) { if remindSwitch.on { let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: []) UIApplication.sharedApplication().registerUserNotificationSettings(settings) let date:NSDate = NSDate() let calendar:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! let components:NSDateComponents = calendar.components([.Year, .Month, .Day], fromDate: date) components.hour = Int(remindStepper.value) var time:NSDate = calendar.dateFromComponents(components)! if(time.compare(date) == NSComparisonResult.OrderedAscending) { time = time.dateByAddingTimeInterval(60*60*24) } self.setEveryDayNotification(time) remindOnOff = true NSUserDefaults.standardUserDefaults().setBool(remindOnOff, forKey: "remindOnOff") } else { remindOnOff = false UIApplication.sharedApplication().cancelAllLocalNotifications() NSUserDefaults.standardUserDefaults().setBool(remindOnOff, forKey: "remindOnOff") } } 
  • more. that is, you need, when the user clicks on the button, read what remindLabel has and run, for example, 10 notifications for the next 10 days at the time that was in the label? - Max Mikheyenko pm
  • It is necessary that every day, at that time that the user has installed in the label, an alert comes - Artur Skachkov
  • in other words, you need to convert the set time to NSDate, and then in the cycle run local notifications each time adding to the date 86400 (the number of seconds in a day) - Max Mikheyenko
  • How to implement it? I do not have enough knowledge - Artur Skachkov
  • Is remindStepper a change in value? Apparently then there is still a button to start the entire alert mechanism? - Max Mikheyenko

1 answer 1

Well, here's an example of how this can be implemented - copy into an empty project and run

creates alerts for 365 days in advance at a specified time (hour)

 class ViewController: UIViewController { var label:UILabel! var stepper:UIStepper! override func viewDidLoad() { stepper = UIStepper(frame: CGRectMake(0,100,200,100)) stepper.minimumValue = 0 stepper.maximumValue = 23 stepper.addTarget(self, action: "stepperChanged:", forControlEvents: UIControlEvents.ValueChanged) self.view.addSubview(stepper) label = UILabel(frame: CGRectMake(0,200,200,100)) label.text = "0" self.view.addSubview(label) let button:UIButton = UIButton(frame: CGRectMake(0,300,200,100)) button.setTitle("schedule", forState: UIControlState.Normal) button.backgroundColor = UIColor.grayColor() button.addTarget(self, action: "startAction:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(button) } func stepperChanged(sender:UIStepper) { label.text = String(format: "%.0f", arguments: [sender.value]) } func startAction(sender:UIButton) { let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: []) UIApplication.sharedApplication().registerUserNotificationSettings(settings) let date:NSDate = NSDate() let calendar:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! let components:NSDateComponents = calendar.components([.Year, .Month, .Day], fromDate: date) components.hour = Int(stepper.value) var time:NSDate = calendar.dateFromComponents(components)! if(time.compare(date) == NSComparisonResult.OrderedAscending) { time = time.dateByAddingTimeInterval(60*60*24) } self.setEveryDayNotification(time) } func setEveryDayNotification(date:NSDate){ let notification: UILocalNotification = UILocalNotification() notification.alertTitle = "alert title" notification.alertBody = "alert body" notification.soundName = UILocalNotificationDefaultSoundName notification.alertAction = "Run the workout" notification.timeZone = NSTimeZone.defaultTimeZone() notification.fireDate = date notification.repeatInterval = NSCalendarUnit.Day UIApplication.sharedApplication().scheduleLocalNotification(notification) } } 
  • Maxim, for some reason, when I start up on the device, two identical notifications come at the same time? What's wrong? - Artur Skachkov
  • set a breakpoint in setEveryDayNotification see if it is called twice - Max Mikheyenko
  • I checked it with breakpoint, but I did not find setEveryDayNotification repetitions ... I edited the code above - Artur Skachkov
  • I test further, it turns out on the second day two notifications come, on the third day three notifications and so on ... - Artur Skachkov
  • Maxim, please tell me how to fix it - Artur Skachkov