I am working on an alarm application, I need to schedule an alarm at a specific time, I use scheduleLocalNotification to schedule alarms, and it works just the way I want. BUT I need to execute a request to my API server before starting the alarm. In this query, I want to check some parameters returned from the API server if this satisfies some condition.
If someone has a method that starts on a specific date - time is fast. Please help me for this.
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) } }
source share