Skip to content

Instantly share code, notes, and snippets.

@nguyentruongky
Created April 6, 2018 06:47
Show Gist options
  • Save nguyentruongky/7c3036900b64a377a57cfa957c1843c5 to your computer and use it in GitHub Desktop.
Save nguyentruongky/7c3036900b64a377a57cfa957c1843c5 to your computer and use it in GitHub Desktop.
Add event to calendar
struct ogeSystemCalendar {
let eventStore = EKEventStore()
func addEvent(title: String, startDate: Date,
endDate: Date, notes: String?) {
eventStore.requestAccess(to: .event) { (granted, error) in
if granted == false {
DispatchQueue.main.async {
self.tellNoPermission() }
return
}
self.addToCalendar(title: title, start: startDate,
end: endDate, notes: notes)
}
}
func tellNoPermission() {
let alert = ogeMessage.showDialog(title: "no_permission".i18n, description: "no_calendar_permission".i18n)
alert.addAction(PMAlertAction(title: "OK", style: .default, action: {
DispatchQueue.main.async { ogeSystemInteractor.openCalendar() }
}))
appDelegate.ogeniiManager?.present(alert)
}
private func addToCalendar(title: String, start startDate: Date,
end endDate: Date, notes: String?) {
let event = EKEvent(eventStore: eventStore)
event.title = title
event.startDate = startDate
event.endDate = endDate
event.notes = notes
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
DispatchQueue.main.async {
ogeMessage.showMessage("saved_to_calendar".i18n) }
} catch { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment