2016-04-12 2 views
2

Я пытаюсь запланировать UILocalNotifications. Функциональность, которую я хочу реализовать, заключается в том, чтобы повторять уведомление в суточный день, например (каждый понедельник), который будет выбран пользователем. Есть ли способ запланировать UILocalNotification, чтобы уведомление включалось только в тот день.Расписание UILocalNotifiation в тот же день swift

let notification = UILocalNotification() 

let dict:NSDictionary = ["ID" : "your ID goes here"] 
notification.userInfo = dict as! [String : String] 
notification.alertBody = "\(title)" 
notification.alertAction = "Open" 
notification.fireDate = dateToFire 
notification.repeatInterval = .Day // Can be used to repeat the notification 
notification.soundName = UILocalNotificationDefaultSoundName 
UIApplication.sharedApplication().scheduleLocalNotification(notification) 
+0

Да, сначала создайте свои кнопки, чтобы выбрать день, в который вы хотите запустить LocalNotification. – Khuong

ответ

1

Сделать это еженедельно делать:

notification.repeatInterval = .Weekday // Or maybe .WeekOfYear 

Независимо от дня недели dateToFire есть, уведомление будет повторяться один раз в неделю в тот же день недели.

1

Я не уверен, что это поможет вам, но я работал с этим раньше. Я создаю 7 кнопок для выбора 7 дней в неделю. Это мой старый код:

class RemindNotification { 

    var timerNotification = NSTimer() 

    func notification(story: Story) { 
    let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
    let date = NSDate() 

    let dateComponents = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date) 
    dateComponents.hour = story.remindeAtHour 
    dateComponents.minute = story.remindeAtMinute 
    for index in 0..<story.remindeAtDaysOfWeek.count { 
     if story.remindeAtDaysOfWeek[index] == true { 
     if index != 6{ 
      dateComponents.weekday = index + 2 
      fireNotification(calendar!, dateComponents: dateComponents) 
     } else { 
      dateComponents.weekday = 1 
      fireNotification(calendar!, dateComponents: dateComponents) 
     } 
     } else { 
     dateComponents.weekday = 0 
     } 
    } 
    } 

    func fireNotification(calendar: NSCalendar, dateComponents: NSDateComponents) { 
    let notification = UILocalNotification() 
    notification.alertAction = "Title" 
    notification.alertBody = "It's time to take a photo" 
    notification.repeatInterval = NSCalendarUnit.WeekOfYear 
    notification.fireDate = calendar.dateFromComponents(dateComponents) 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
    } 
} 

Это для моего приложения, я могу выбрать день, а затем он может стрелять LocalNotification в определенное время, что я поставил в каждый день.

Вы можете обратиться к нему. Надеюсь, эта помощь.

Смежные вопросы