在特定的工作日触发本地通知

时间:2016-05-23 21:50:44

标签: swift nsdate uilocalnotification

我试图在工作日的特定日期触发本地通知,但我的所有尝试都失败了,通知应该在工作日的特定时间触发,但相反它立即被触发,我搜索了所有堆栈溢出的可能解决方案,但没有那些答案解决了我的问题,我怎么能解决这个问题?

func combineDate(time:NSDate,dayIndex:Int) -> NSDate {

    let calendar: NSCalendar = NSCalendar.currentCalendar()
    let components: NSDateComponents = calendar.components([.Year, .Month, .WeekOfYear, .Weekday], fromDate: time)
    let component1 = NSDateComponents()

    component1.weekday = dayIndex
    component1.minute = components.minute
    component1.hour = components.hour
    let combinedDate: NSDate = calendar.dateFromComponents(component1)!

    return combinedDate
}

1 个答案:

答案 0 :(得分:0)

您只需在本地通知中设置fireDate属性即可。 创建自定义日期的一种方法可以是NSDateFormatter

let stringDate = "2016-05-23 17:29:50 +0000"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let myDate = formatter.dateFromString(stringDate)


func saveLocalNotification(myDate : NSDate){

  let localNotification = UILocalNotification()
  localNotification.fireDate = myDate
  ...
  UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}

要了解如何获得工作日,您可以查看此答案Get day of week using NSDate swift