UILocalNotification每周重复一次

时间:2016-07-12 16:23:07

标签: ios swift notifications uilocalnotification nscalendar

我想设置UILocalNotification,每周五每周自动重复一次。我目前正在使用下面的代码同时每天重复一次通知,但我不知道如何在本周创建此通知。

谢谢

let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
                let now: NSDate! = NSDate()
                let notifDate = calendar.dateBySettingHour(19, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!

                var notification = UILocalNotification()
                notification.category = "Reminder"
                notification.alertTitle = "Alert"
                notification.alertBody = "Alert body"
                notification.fireDate = notifDate
                notification.soundName = UILocalNotificationDefaultSoundName
                notification.repeatInterval = NSCalendarUnit.Day
                UIApplication.sharedApplication().scheduleLocalNotification(notification)

1 个答案:

答案 0 :(得分:1)

点击此处Apple Docs查看UILocalNotification

您可以将其设置为每周重复一次。

notification.repeatInterval = NSCalendarUnit.WeekOfYear;

PS。此代码将在创建通知的那一天创建一个通知,而不仅仅是在星期五。需要考虑的事情。

let now: NSDate! = NSDate() //whatever today's day is and it could be any day between M-Sun
let notifDate = calendar.dateBySettingHour(19, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!