Skip to content

Instantly share code, notes, and snippets.

@mchirico
Created September 6, 2014 12:36
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mchirico/d072c4e38bda61040f91 to your computer and use it in GitHub Desktop.
Save mchirico/d072c4e38bda61040f91 to your computer and use it in GitHub Desktop.
Example of creating and removing calendar entries in Swift. Also, shows how to list reminders
/*
You need to import EventKit
import EventKit
*/
@IBAction func buttonCalendar(sender: AnyObject) {
var eventStore : EKEventStore = EKEventStore()
// 'EKEntityTypeReminder' or 'EKEntityTypeEvent'
eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: {
granted, error in
if (granted) && (error == nil) {
println("granted \(granted)")
println("error \(error)")
var event:EKEvent = EKEvent(eventStore: eventStore)
event.title = "Test Title"
event.startDate = NSDate()
event.endDate = NSDate()
event.notes = "This is a note"
event.calendar = eventStore.defaultCalendarForNewEvents
eventStore.saveEvent(event, span: EKSpanThisEvent, error: nil)
println("Saved Event")
}
})
// This lists every reminder
var predicate = eventStore.predicateForRemindersInCalendars([])
eventStore.fetchRemindersMatchingPredicate(predicate) { reminders in
for reminder in reminders {
println(reminder.title)
}}
// What about Calendar entries?
var startDate=NSDate().dateByAddingTimeInterval(-60*60*24)
var endDate=NSDate().dateByAddingTimeInterval(60*60*24*3)
var predicate2 = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
println("startDate:\(startDate) endDate:\(endDate)")
var eV = eventStore.eventsMatchingPredicate(predicate2) as [EKEvent]!
if eV != nil {
for i in eV {
println("Title \(i.title)" )
println("stareDate: \(i.startDate)" )
println("endDate: \(i.endDate)" )
if i.title == "Test Title" {
println("YES" )
// Uncomment if you want to delete
//eventStore.removeEvent(i, span: EKSpanThisEvent, error: nil)
}
}
}
}
@dhegyi
Copy link

dhegyi commented Oct 12, 2014

Hi,

This is great! I'd like to write an app that lists my reminders by sorting them (and even filter them) in different ways. Also, I'd like to be able to set certain reminders to completed.

How can I do this?

Thanks,
Daniel

@Jason-Robinson
Copy link

Do you have any way to handle if the user denies the request? For me once they select cancel I can't change the status and resubmit the request.

@mstolin
Copy link

mstolin commented May 22, 2015

Its much faster in a background thread.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    // code here
}

@Terens777
Copy link

thanks

@crucible333
Copy link

I noticed you saved the event with the notes. I was able to do this successfully as well as it shows up in my Calendar. However, if I'm trying to retrieve the data and display it on a UITableView. The only thing that isn't accessible is the notes. Is there a trick to gain access to the notes? I noticed you didn't show how to do it either even though you saved it.

Thanks

Copy link

ghost commented Dec 14, 2016

how to make predicate for title, location,endDate and startDate.if match all things than fetch event.i need in objective c.

@TamilRaja
Copy link

How to update and delete event from ekevent calendar in swift?

@ahmadsallam
Copy link

this error occurs when i try this code " Error getting shared calendar invitations for entity types 3 from daemon " ?
any help

@designablebits
Copy link

Thanks

@carolinecreid
Copy link

What about time zones? Does this take that into consideration?

@BincyOct2021
Copy link

how can i disable the edit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment