UILocalNotification 每周重复一次
Posted
技术标签:
【中文标题】UILocalNotification 每周重复一次【英文标题】:UILocalNotification Repeat Once Weekly 【发布时间】:2016-07-12 16:23:06 【问题描述】:我想设置一个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)
【问题讨论】:
将 repeatInterval 更改为 NSCalendarUnit。年周。它将每周在同一时间重复。 似乎没有“NSCalendarUnit.Week”。有一个错误提示:Type 'NSCalendarUnit' has no member 'Week' 它的“NSCalendarUnit.WeekOfYear” 【参考方案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)!
【讨论】:
当使用关于错误信息的那一行更改为:Use of unresolved identifier 'kCFCalendarUnitWeek' 试试 - NSWeekCalendarUnit以上是关于UILocalNotification 每周重复一次的主要内容,如果未能解决你的问题,请参考以下文章