2016-07-30 4 views
0

У меня проблема с моим кодом.Как установить еженедельное локальное уведомление в swift

Я хочу установить локальное уведомление в xcode7, я разрабатываю календарь, где вы можете поместить курсы своего университета, дело в том, что я получаю расписание из базы данных json, и хочу сообщить за 15 минут до класс начинается, но я не знаю, почему мой код не работает.

Это пример, когда я хочу повторять уведомление каждый понедельник в 13:40.

Могу ли я установить только день и час? или я должен указать месяц и год?

var dateComp:NSDateComponents = NSDateComponents() 

    dateComp.day = 01; 
    dateComp.hour = 13; 
    dateComp.minute = 40; 
    dateComp.timeZone = NSTimeZone.systemTimeZone() 

    var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! 
    var date:NSDate = calender.dateFromComponents(dateComp)! 

    let notification = UILocalNotification() 
    notification.fireDate = date 
    notification.alertBody = "Swipe to unlock" 
    notification.alertAction = "You've got a class soon!" 
    notification.soundName = UILocalNotificationDefaultSoundName 
    notification.userInfo = ["CustomField1": "w00t"] 
    notification.repeatInterval = NSCalendarUnit.WeekOfYear 

    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
+0

Вы должны установить часовой пояс уведомления местных –

+0

http://stackoverflow.com/a/34575836/2303865 –

ответ

0

пожалуйста, проверьте эту функцию

func setLNotification(weekDay:Int , hour:Int, min:Int, second:Int, alertBody:String, type:String, isRepeate:Bool){ 
    let calender = NSCalendar(identifier: NSCalendarIdentifierGregorian) 
    let dateComp: NSDateComponents? 
    let components: NSDateComponents = NSDateComponents() 

    if weekDay > 0{ 
     components.setValue(-50, forComponent: NSCalendarUnit.Year) 

     let previousDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))! 
     dateComp = calender?.components([.Year,.WeekOfMonth,.Month], fromDate: previousDate) 
     dateComp?.hour = hour 
     dateComp?.minute = min 
     dateComp?.second = second 
     dateComp?.weekday = weekDay 
    }else{ 
     components.setValue(hour, forComponent: NSCalendarUnit.Hour) 
     components.setValue(min, forComponent: NSCalendarUnit.Minute) 
     components.setValue(second, forComponent: NSCalendarUnit.Second) 
     let notifiDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))! 
     dateComp = calender?.components([.Year,.Month,.Day,.Hour,.Minute,.Second], fromDate: notifiDate) 
    } 

    let notification = UILocalNotification() 
    if isRepeate == true{ 
     notification.repeatInterval = NSCalendarUnit.WeekOfYear 
     notification.repeatCalendar = calender 
    } 
    notification.fireDate = calender?.dateFromComponents(dateComp!) 
    notification.alertBody = alertBody 
    notification.userInfo = ["day":"\(weekDay)","type":"\(type)"] 

    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
} 
Смежные вопросы