安排每周通知SWIFT的麻烦

时间:2016-05-20 18:02:09

标签: swift

我每周都要安排通知安排。我的问题是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);
}

1 个答案:

答案 0 :(得分:0)

您的问题的答案是:

notification.repeatInterval = NSCalendarUnit.weekOfYear

因为iOS 8.0中已弃用NSWeekOfYearCalendarUnit

如果您想找到今年正确的一周,您可以执行以下操作

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(答案当天)