@artursk

Задание времени для UILocalNotification используя UIStepper?

Необходимо отправлять напоминания раз в сутки, по установленному времени. Время устанавливаю в формате от 1 до 24 часов при использовании UIStepper. Отредактировал код, но все равно не работает-УВЕДОМЛЕНИЯ НЕ ПРИХОДЯТ
var hour = 0.0
let notification = UILocalNotification()
@IBOutlet weak var remindStepper: UIStepper!
@IBOutlet weak var remindLabel: UILabel!

Во 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"
    }

далее создаем
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() //ДОБАВЛЯЮ УВЕДОМЛЕНИЕ
}
}
  • Вопрос задан
  • 122 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы