Setting Multiple Times for Notifications in Swift

后端 未结 1 1406
慢半拍i
慢半拍i 2020-12-04 03:44

So I am using Swift now and I had one notification appearing at a certain time when one switch was activated.

However I would like another notification to come up at

相关标签:
1条回答
  • 2020-12-04 04:49
    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet var myDatePicker: UIDatePicker!
        @IBOutlet var mySwitch7am: UISwitch!
        @IBOutlet var mySwitch8am: UISwitch!
    
        var localNotification7am = UILocalNotification()
        var localNotification8am = UILocalNotification()
        var notificationsCounter = 0
    
        func datePicker()            { myDatePicker.datePickerMode = UIDatePickerMode.Date }
        func datePickerDefaultDate() { myDatePicker.date = NSDate().hour > 8 ? NSDate().xDays(+1) : NSDate() }
    
        func notificationsOptions7am() {
            localNotification7am.timeZone = NSTimeZone.localTimeZone()
            localNotification7am.repeatInterval = .CalendarUnitDay
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification7am)
            localNotification7am.alertAction = "Open App"
            localNotification7am.soundName = UILocalNotificationDefaultSoundName
        }
        func notificationsOptions8am() {
            localNotification7am.timeZone = NSTimeZone.localTimeZone()
            localNotification7am.repeatInterval = .CalendarUnitDay
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification8am)
            localNotification7am.alertAction = "Open App"
            localNotification7am.soundName = UILocalNotificationDefaultSoundName
        }
        func toggleSwitch7am(){
            localNotification7am.alertBody = "Here is the seven o'clock notification"
            localNotification7am.fireDate = mySwitch7am.on ? myDatePicker.date.fireDateAt7am         : NSDate().xDays(+36500)
        }
        func toggleSwitch8am(){
            localNotification8am.alertBody = "Here is the eight o'clock notification"
            localNotification8am.fireDate = mySwitch8am.on ? myDatePicker.date.fireDateAt8am         : NSDate().xDays(+36500)
        }
        override func viewDidLoad() {
            super.viewDidLoad()
            datePicker()
            datePickerDefaultDate()
            notificationsOptions7am()
            notificationsOptions8am()
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
        @IBAction func switchedOn7am(sender: AnyObject) {
            toggleSwitch7am()
        }
        @IBAction func switchedOn8am(sender: AnyObject) {
            toggleSwitch8am()
        }
    }
    

    Extensions:

        import Foundation
        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 fireDateAt7am: NSDate    { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 7, minute: 0, second: 0, nanosecond: 0)! }
            var fireDateAt8am: NSDate    { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 8, minute: 0, second: 0, nanosecond: 0)! }
            func fireDateAt(hr:Int, min:Int) -> NSDate {
                return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: hr, minute: min, second: 0, nanosecond: 0)!
            }
        }
    
    0 讨论(0)
提交回复
热议问题