import UIKitclass 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() }}
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条)