I’m not a very experienced Swift programmer, and I’m trying to work this out from pieces I’ve picked up on SO and other places.
I’m trying to add a simple calendar event on iOS. I’ve got a button in SwiftUI, which looks like this:
Button(action: {
let eventStore : EKEventStore = EKEventStore()
var ok = false
let authorizationStatus = EKEventStore.authorizationStatus(for: .event)
if authorizationStatus == .fullAccess {
print("Already Authorised")
ok = true
}
else {
eventStore.requestFullAccessToEvents { granted, error in
print("granted: \(Date())")
ok = granted && error==nil
}
}
if(ok) {
let event:EKEvent = EKEvent(eventStore: eventStore)
event.timeZone = nil
event.title = "Testing"
event.startDate = Date()
event.endDate = Date()
event.notes = "This is a test"
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
print("Saved")
} catch let error as NSError {
print("oops: \(error)")
}
}
else {
print("no good")
}
}) {
Text("Test")
}
The code runs and prints the right messages, but it doesn’t actually save the event in the calendar.
In the console, I get the message:
XPC connection was invalidated
and I don’t know what that means or whether it’s related.
Whats needed to get the calendar event to save?