I've created a function in swift that checks through all the Calendars currently saved on an iOS device, then creates a new one if a calendar with the specific title "TESTCAL" (in this case) doesn't exist.
func checkCal(){
print("checkCal")
let calendars = eventStore.calendars(for: EKEntityType.event) as [EKCalendar]
var exists = false
for calendar in calendars {
if calendar.title == "TESTCAL"{
exists = true
print("FOUND CAL: TESTCAL")
print(calendar.title)
print(calendar.calendarIdentifier)
self.calIdent = calendar.calendarIdentifier
} else{
print("NO CAL: ", calendar.title)
}
}
if exists == false {
let newCal = EKCalendar(for: EKEntityType.event, eventStore: eventStore)
newCal.title = "TESTCAL"
newCal.source = eventStore.defaultCalendarForNewEvents?.source
_ = try? eventStore.saveCalendar(newCal, commit: true)
print("CAL CREATED: ", newCal.title)
print("With Ident: ", newCal.calendarIdentifier)
self.calIdent = newCal.calendarIdentifier
}
addToCal()
}
Then when I come to add the event later, in the following way
func addToCal()
{
let eventVC = EKEventEditViewController()
eventVC.editViewDelegate = self
eventVC.eventStore = EKEventStore()
let date = self.passedDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yy"
let dateForCal = dateFormatter.date(from: date)
let event = EKEvent(eventStore: eventVC.eventStore)
event.title = self.passedTitle
event.notes = self.passedDetail
event.startDate = dateForCal
event.endDate = dateForCal
event.isAllDay = true
event.url = URL(string: passedWebLink) }
The event gets added to the iOS default calendar.
I've tried adding the following code to the addCal() function:
event.calendar = eventStore.calendar(withIdentifier: self.calIdent)
and
let calendars = eventStore.calendars(for: EKEntityType.event) as [EKCalendar]
for calendar in calendars {
if calendar.title == "TESTCAL"{
event.calendar = calendar
print("YES")
} else{
print("NO")
}
}
Both of these give me error message in the AppDelegate and cause a crash:
[EventKit] Error getting shared calendar invitaions for entity types 3 from daemon: Error Domain=EKCADErrorDomain Code 1014 "(null)
Any ideas?
I see more possible causes for the issue you are experiencing:
The TESTCAL calendar is not created successfully. You can validate the calendar creation by catching the possible error thrown on saving the calendar:
How the event saving is happening is not clear from the code you posted.
If you are presenting the EKEventEditViewController instance for the user to complete the event creation, you are missing setting the event on the view controller in the addToCal method:
eventVC.event = event
If you want to save the event directly, then you don't need to use EKEventEditViewController, but instead call save on the eventStore:
For shared calendars, an additional privacy key for contacts accessing is needed in the .plist file of your target, namely NSContactsUsageDescription. Without this key, accessing shared calendars causes a crash.
With everything from the above checked/taken care of, your solution of setting the calendar on the event should work:
Also, make sure to have in place the code for requesting event kit store access from the user before any calendar/event manipulation. For more info on requesting access, check https://developer.apple.com/documentation/eventkit/ekeventstore/1507547-requestaccess