Search code examples
iosswiftxcodeeventkit

How to add alert for events in iOS eventKit?


I am able to successfully create an event using iOS eventkit, but the event is saved without alert. Is is possible to add alert(like 15 mins before, 1hour before etc) to calendar's event from code? This is my current code to create event

  let eventStore : EKEventStore = EKEventStore()
  // 'EKEntityTypeReminder' or 'EKEntityTypeEvent'
  eventStore.requestAccess(to: .event) { (granted, error) in
      let event:EKEvent = EKEvent(eventStore: eventStore)
      if let title = self.calendarTitle {
        event.title = title  
      } else {
        event.title = "Session"
      }
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
      let dateStart = dateFormatter.date(from: self.calendarStart)
      let dateEnd =  dateFormatter.date(from: self.calendarEnd)
      event.startDate = dateStart!
      event.endDate = dateEnd!
      event.notes = self.calendarNote
      event.calendar = eventStore.defaultCalendarForNewEvents
      do {
        try eventStore.save(event, span: .thisEvent)
      } catch let error as NSError {
        print("failed to save event with error : \(error)")
      }
      self.alertify(message: "Event saved in Calendar", in: self, success: true)
    }
    else {
      self.alertify(message: "Unable to save", in: self, success: false)
    }
  }

Solution

  • You can check EKAlarm documentation:

    EKAlarm Apple doc

    You can use its absoluteDate property to set the alarm for your event.