我对安排每周取消通知有问题。我的问题是notification.repeatInterval应该等于什么才能工作。谢谢!
func applicationDidEnterBackground(application: UIApplication)
{
let state = NSUserDefaults.standardUserDefaults().boolForKey("notifications_enabled");
if (!state) {
UIApplication.sharedApplication().cancelAllLocalNotifications()
}
else {
UIApplication.sharedApplication().cancelAllLocalNotifications()
for var x = 2; x <= 3; x = x+1
{
createAlert(7, minute: 55, day: x, period: "Period 1 Starts in 5 Minutes");
createAlert(8, minute: 45, day: x, period: "Period 2 Starts in 5 Minutes");
createAlert(9, minute: 30, day: x, period: "Break Has Started");
createAlert(10, minute: 10, day: x, period: "Period 3 Starts in 5 Minutes");
createAlert(11, minute: 00, day: x, period: "Period 4 Starts in 5 Minutes");
createAlert(11, minute: 50, day: x, period: "Period 5 Starts in 5 Minutes");
createAlert(12, minute: 40, day: x, period: "Lunch Has Started");
createAlert(13, minute: 51, day: x, period: "Period 6 Starts in 5 Minutes");
createAlert(14, minute: 00, day: x, period: "Period 7 Starts in 5 Minutes");
createAlert(14, minute: 50, day: x, period: "Period 8 Starts in 5 Minutes");
}
}
}
func createAlert(hour:Int, minute:Int, day:Int, period:String)
{
let dateComp:NSDateComponents = NSDateComponents()
dateComp.hour = hour;
dateComp.minute = minute;
dateComp.weekday = day;
dateComp.timeZone = NSTimeZone.systemTimeZone()
let calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let date:NSDate = calender.dateFromComponents(dateComp)!
let notification:UILocalNotification = UILocalNotification()
notification.category = "Daily Quote"
notification.alertBody = period
notification.alertAction = "view"
notification.fireDate = date
notification.soundName = "dingdongloud.mp3"
notification.repeatInterval = NSCalendarUnit.NSWeekOfYearCalendarUnit;
UIApplication.sharedApplication().scheduleLocalNotification(notification);
}
最佳答案
你的问题的答案是:notification.repeatInterval = NSCalendarUnit.weekOfYear
因为NSWeekOfYearCalendarUnit
在iOS 8.0中被弃用。
如果你想找到今年正确的一周,你可以做以下事情
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.WeekOfYear, fromDate: date)
print(components.weekOfYear)
22(答题当天)
如果出于某种原因,您希望将此值转换为
NSCalendarUnit
,请执行以下操作:let week = UInt(components.weekOfYear)
NSCalendarUnit(rawValue: week)
注意:
NSCalendarUnit.weekOfYear.rawValue
将为您提供一个一年中一周的位掩码。8192(答卷当天)