2

I'm trying to trigger local notification on specific day of weekday, but all my attempts have failed, the notification should be triggered on specific time of weekday, but instead it's triggered immediately, I searched all possible solutions in stack overflow but non of those answers have solved my problem, how I can solve this problem?

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
}
User
  • 123
  • 4
  • 13

1 Answers1

0

You just have to set the fireDate attribute in the local notification. One way to create your custom date could be with 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)
}

To know how to get the weekdays you could take a look at this answer Get day of week using NSDate swift

Community
  • 1
  • 1
Rafaalvfe
  • 73
  • 1
  • 1
  • 8
  • well I know about `fireDate`, and I want to trigger notification on specific day of week "Monday, Tuesday, etc" not on specific date – User May 23 '16 at 22:19
  • Then you need to work out what date corresponds to that day of the week, and schedule the notification for that date. – Tom Harrington May 24 '16 at 16:19