A
A
artursk2016-04-10 15:54:10
iOS
artursk, 2016-04-10 15:54:10

Time setting for UILocalNotification using UIStepper?

It is necessary to send reminders once a day, at the set time. I set the time in the format from 1 to 24 hours when using UIStepper. I edited the code, but it still does not work - NOTIFICATIONS DO NOT COME

var hour = 0.0
let notification = UILocalNotification()
@IBOutlet weak var remindStepper: UIStepper!
@IBOutlet weak var remindLabel: UILabel!

In viewDidLoad()
remindStepper.wraps = true
    remindStepper.value = hour
    remindStepper.minimumValue = 0
    remindStepper.maximumValue = 24
    //ЧТЕНИЕ УСТАНОВЛЕННОГО ВРЕМЕНИ УВЕДОМЛЕНИЯ
    if NSUserDefaults.standardUserDefaults().objectForKey("hour") != nil  { hour = NSUserDefaults.standardUserDefaults().objectForKey("hour") as! Double }
    if remindStepper.value == 0 {
        remindLabel.text = "Off"
    } else {
        remindLabel.text = "\(Int(hour)):00"
    }

then we create
func setEveryDayNotification(){

let calendar = NSCalendar.currentCalendar()
var dateFire = NSDate()
let components = calendar.components(.Hour,fromDate: dateFire)
components.calendar = calendar
components.hour = Int(hour)
dateFire = calendar.dateFromComponents(components)!

notification.alertTitle = "alert title"
notification.alertBody = "alert body"
notification.soundName = UILocalNotificationDefaultSoundName
notification.alertAction = "Run the workout"
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.fireDate = components.date
notification.repeatInterval = NSCalendarUnit.Day
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
@IBAction func remindStepper(sender: UIStepper) {

if remindStepper.value == 0 {
    NSUserDefaults.standardUserDefaults().setObject(Int(sender.value), forKey: "hour")
    NSUserDefaults.standardUserDefaults().synchronize()
    remindLabel.text = "Off"
    UIApplication.sharedApplication().cancelLocalNotification(notification)// ЗДЕСЬ ПЫТАЮСЬ УДАЛИТЬ УВЕДОМЛЕНИЕ
} else {
    NSUserDefaults.standardUserDefaults().setObject(Int(sender.value), forKey: "hour")
    NSUserDefaults.standardUserDefaults().synchronize()
    remindLabel.text = "\(Int(sender.value).description):00"
    setEveryDayNotification() //ДОБАВЛЯЮ УВЕДОМЛЕНИЕ
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question