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") } }