Reputation: 588
I've seen many people simply search the user's calendars and then grab the first one that's writable and start throwing the events on there. I don't like this practice; I prefer to respect my users a little more.
So how can you actually make your own calendar for the app to use for the events that it creates?
Upvotes: 0
Views: 717
Reputation: 588
I came up with the following function, assuming your app is allowed to access the users calendars:
let eventStore = EKEventStore()
func checkCalendar() -> EKCalendar {
var retCal: EKCalendar?
let calendars = eventStore.calendarsForEntityType(EKEntityTypeEvent) as! [EKCalendar] // Grab every calendar the user has
var exists: Bool = false
for calendar in calendars { // Search all these calendars
if calendar.title == "Any String" {
exists = true
retCal = calendar
}
}
var err : NSError?
if !exists {
let newCalendar = EKCalendar(forEntityType:EKEntityTypeEvent, eventStore:eventStore)
newCalendar.title="Any String"
newCalendar.source = eventStore.defaultCalendarForNewEvents.source
eventStore.saveCalendar(newCalendar, commit:true, error:&err)
retCal = newCalendar
}
return retCal!
}
This function looks for a calendar by the name of "Any String". Of course, this can be anything you like. If it doesn't exist, it will create it. It then returns the calendar, so you can assign it to a calendar variable and then work with it however you please.
Upvotes: 1