2016-05-20 13 views
1

毎週通知をスケジュールするスケジュールに問題があります。私の質問は、それが機能するためには、notification.repeatIntervalと同等でなければならないことです。ありがとう!週間通知のスケジュールに関する問題SWIFT

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); 
} 
+0

あなたは 'NSCalendarUnitWeekOfYear'カレンダー単位でみてくださいましたか? – Desdenova

答えて

0

あなたの質問への答えは次のとおりです。

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(答えの日のための)

とIFを行うことができますこの値をNSCalendarUnitにキャストしたい場合:

let week = UInt(components.weekOfYear) 
NSCalendarUnit(rawValue: week) 

注:NSCalendarUnit.weekOfYear.rawValueは、のビットマスクを週に与えるでしょう。 (答えの日のための)

+0

「NSCalendarUnit型にint型の値を代入できません」 –

+0

'components.weekOfYear'行は、今年の週を' Int'として返します。その値を保存することができ、週が変更された場合にのみ通知を送信します。 – Idan

+0

'notification.repeatInterval = NSCalendarUnit.weekOfYear'を試しましたか? – Idan