3

i want set as local notification fireDate the date of my datePicker. I found that code from another answer at S.O.:

      @IBOutlet var myDatePicker: UIDatePicker!
      @IBOutlet var mySwitch: UISwitch!

var localNotification = UILocalNotification()   // You just need one
var notificationsCounter = 0

 // put your functions now
 func datePicker()            { myDatePicker.datePickerMode = UIDatePickerMode.Time }
func notificationsOptions()  {
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.repeatInterval = .CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
localNotification.alertAction = "Open App"
localNotification.alertBody = "Here is the seven o'clock notification"
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
//     you may add arbitrary key-value pairs to this dictionary.
//     However, the keys and values must be valid property-list types
//     if any are not, an exception is raised.
// localNotification.userInfo = [NSObject : AnyObject]?
}
  func toggleSwitch(){
if mySwitch.on{
    localNotification.fireDate = myDatePicker.date
} else {
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999)          // will never be fired
}
  }
override func viewDidLoad() {
super.viewDidLoad()
datePicker()
notificationsOptions()
// Do any additional setup after loading the view, typically from a nib.
 }

But it doesn't work, even if all seems correct...where is the problem??

Community
  • 1
  • 1
Swift1
  • 349
  • 1
  • 4
  • 22

2 Answers2

3

Try like this:

class ViewController: UIViewController {

    @IBOutlet var datePicker: UIDatePicker!
    @IBOutlet var notificationSwitch: UISwitch!

    let localNotification = UILocalNotification()

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpNotificationsOptions()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    func setUpNotificationsOptions()  {
        datePicker.datePickerMode = .Time
        localNotification.timeZone = NSTimeZone.localTimeZone()
        localNotification.repeatInterval = .Day
        localNotification.alertAction = "Open App"
        localNotification.alertBody = "a notification"
        localNotification.soundName = UILocalNotificationDefaultSoundName
    }

    func toggleNotification() {
        if notificationSwitch.on {
            localNotification.fireDate = datePicker.date.fireDate
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
        } else {
            localNotification.fireDate = nil
            UIApplication.sharedApplication().cancelLocalNotification(localNotification)
        }
    }
    @IBAction func toggleSwitch(sender: UISwitch) {
       toggleNotification()
    }
    @IBAction func dateChanged(sender: UIDatePicker) {
       toggleNotification()
    }
}

you will need those extensions:

extension NSDate {
    var minute: Int {
        return NSCalendar.currentCalendar().component(.Minute, fromDate: self)
    }
    var hour: Int {
        return NSCalendar.currentCalendar().component(.Hour, fromDate: self)
    }
    var day: Int {
        return NSCalendar.currentCalendar().component(.Day, fromDate: self)
    }
    var month: Int {
        return NSCalendar.currentCalendar().component(.Month, fromDate: self)
    }
    var year: Int {
        return NSCalendar.currentCalendar().component(.Year, fromDate: self)
    }
    var fireDate: NSDate {
        let today = NSDate()
        return NSCalendar.currentCalendar().dateWithEra(1,
            year: today.year,
            month: today.month,
            day: { hour > today.hour || (hour  == today.hour
               &&  minute > today.minute) ? today.day : today.day+1 }(),
            hour: hour,
            minute: minute,
            second: 0,
            nanosecond: 0
            )!
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Thank you man!!! "it just works"!!!!!! you are very great, you saved my day, i'm in your debt for this! – Swift1 Dec 13 '15 at 10:29
  • Now i have a problem: all that code is into a "Settings" controller...but i need that localNotification.alertBody value is the content of an array that is in the main viewController. if i pass this value i get that error: use of unresolved identifier...how can i pass this limitation? It's possibile to declare a new indexPath in settings controller? – Swift1 Dec 13 '15 at 12:35
  • @Swift1 feel free to open another question and post the relative code and explanation there and if you would like me to take a look you can post the link here. – Leo Dabus Dec 13 '15 at 15:10
  • Ok i opened a new question here:http://stackoverflow.com/questions/34257339/swift-save-an-array-of-strings-to-nsuserdefaults-and-reuse-for-notification-aler please help me! thank you – Swift1 Dec 13 '15 at 22:50
  • You know how to store datePicker value to NSuserdefaults? – Swift1 Dec 14 '15 at 21:51
  • Hey, i want that if i change date in picker view when i relaunch app date is the same of my last selection...can you help me? i was confused with http://stackoverflow.com/a/29126854/2303865 example – Swift1 Dec 14 '15 at 21:58
  • @Swift1 the comments area it is for comments, not questions. Open a new question asking how to make a datePicker value persist through launches and let me know when you are finished posting it. – Leo Dabus Dec 14 '15 at 22:06
  • Here is my new question http://stackoverflow.com/questions/34277619/swift-save-last-datepicker-selection – Swift1 Dec 14 '15 at 22:15
  • @Swift1 inside the method dateChanged add NSUserDefaults().setDate(sender.date, forKey: "datePicker") – Leo Dabus Dec 14 '15 at 22:18
0

It looks like the fireDate property is only being set inside the toggleSwitch() method. How is that method being reached? I would suggest you move the content of that method right into your notificationsOptions() method.

As an aside, you might want to consider slightly more useful names for your methods: datePicker() and notificationOptions() are likely to cause you maintenance headaches later on because they aren't very descriptive.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71