So now I use Swift, and I got one notification at a certain time when one switch was activated.
However, I would like another notification to appear at a different time when another switch is activated.
Here is my code for ViewController:
@IBOutlet var myDatePicker: UIDatePicker! @IBOutlet var mySwitch: UISwitch! @IBOutlet var mySwitchTwo: UISwitch! var localNotification:UILocalNotification = UILocalNotification() var localNotificationTwo:UILocalNotification = UILocalNotification() func datePicker() { myDatePicker.datePickerMode = UIDatePickerMode.Date } func datePickerDefaultDate() { myDatePicker.date = NSDate().xDays(+1) } func toggleSwitch(){ if mySwitch.on{ localNotification.alertAction = "Open App" localNotification.alertBody = "Please take your medication." localNotification.fireDate = myDatePicker.date.fireDate localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay localNotification.timeZone = NSTimeZone.localTimeZone() UIApplication.sharedApplication().scheduleLocalNotification(localNotification) } else { localNotification.alertAction = "Open App" localNotification.alertBody = "This notification should not appear" localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999) } } func toggleSwitchTwo() { if mySwitchTwo.on{ localNotification.alertAction = "Open App" localNotification.alertBody = "Please take your medication." localNotification.fireDate = myDatePicker.date.fireDateTwo localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay localNotification.timeZone = NSTimeZone.localTimeZone() UIApplication.sharedApplication().scheduleLocalNotification(localNotificationTwo) } else { localNotification.alertAction = "Open App" localNotification.alertBody = "This notification should not appear" localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999) } } override func viewDidLoad() { super.viewDidLoad()
Here is the first fast file with time for the first switch:
import Foundation public extension NSDate { func xDays(x:Int) -> NSDate { return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)! } var day: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay, fromDate: self).day } var month: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth, fromDate: self).month } var year: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: self).year } var fireDate: NSDate { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 8, minute: 0, second: 0, nanosecond: 0)! } }
And I would like the next notification time to be at 13:00. Here is the quick file I made for this:
import Foundation public extension NSDate { func xDaysTwo(x:Int) -> NSDate { return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)! } var dayTwo: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay, fromDate: self).day } var monthTwo: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth, fromDate: self).month } var yearTwo: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: self).year } var fireDateTwo: NSDate { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 13, minute: 0, second: 0, nanosecond: 0)! } }
I am having problems with this and I will be very grateful for your help, thanks in advance!