-
-
Save bernardonigbinde/3decb34cdcfda210c5caa77250206feb to your computer and use it in GitHub Desktop.
Example of creating and removing calendar entries in Swift. Also, shows how to list reminders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) | |
} | |
} | |
} | |
} |
Author
bernardonigbinde
commented
Mar 22, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment