title: How to Run a Task in swift on a particular date-time in background either application is on or off
body: <p>I am working on alarm application, i need to schedule alarm on specific time, I use <code>scheduleLocalNotification</code> for scheduling alarms and it's working fine as i want. <strong>BUT</strong> I need to run to a request to my API server before triggering alarm. In that request I want to check some parameters returning from API server, If that satisfies some condition.</p>
  
  <p>If any one have a method that run on a particular date - time in swift 
  Please help me for that </p>
  
  <pre><code> func addAlarm (newAlarm: Alarm) {
      // Create persistent dictionary of data
      var alarmDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ALARMS_KEY) ?? Dictionary()
      // Copy alarm object into persistent data
      alarmDictionary[newAlarm.UUID] = newAlarm.toDictionary()
      // Save or overwrite data
      NSUserDefaults.standardUserDefaults().setObject(alarmDictionary, forKey: ALARMS_KEY)
  
      scheduleNotification(newAlarm, category: "ALARM_CATEGORY")
      scheduleNotification(newAlarm, category: "FOLLOWUP_CATEGORY")
  
  }
  
      /* NOTIFICATION FUNCTIONS */
  func scheduleNotification (alarm: Alarm, category: String) {
      let notification = UILocalNotification()
      notification.category = category
      notification.repeatInterval = NSCalendarUnit.Day
      switch category {
      case "ALARM_CATEGORY":
          notification.userInfo   = ["UUID": alarm.UUID]
          notification.alertBody  = "Time to wake up!"
          notification.fireDate   = alarm.wakeup
          notification.timeZone   = NSTimeZone.localTimeZone()
          notification.soundName  = "loud_alarm.caf"
          break
      case "FOLLOWUP_CATEGORY":
          notification.userInfo   = ["UUID": alarm.followupID]
          notification.alertBody  = "Did you arrive yet?"
          notification.fireDate   = alarm.arrival
          notification.timeZone   = NSTimeZone.localTimeZone()
          notification.soundName  = UILocalNotificationDefaultSoundName
          break
      default:
          print("ERROR SCHEDULING NOTIFICATION")
          return
      }
      print("Notification=\(notification)")
      // For debugging purposes
      if alarm.isActive {
          UIApplication.sharedApplication().scheduleLocalNotification(notification)
      }
  }
  </code></pre>