I am trying to understand how to subscribe to EventKit calendar event updates within SwiftIU.
The EventKit documentation shows how to do this in Swift (https://developer.apple.com/documentation/eventkit/updating_with_notifications).
It says to subscribe to calendar update notifications by writing:
NotificationCenter.default.addObserver(self, selector: Selector("storeChanged:"), name: .EKEventStoreChanged, object: eventStore)
It also appears that the storeChanged
function needs do be tagged with @objc
based on xcode warnings. However, when I do the below within a SwiftUI app, I get the error @objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
import SwiftUI
import EventKit
struct ContentView: View {
let eventStore = EKEventStore()
var body: some View {
Text("Hello, world!")
.padding()
.onAppear{
NotificationCenter.default.addObserver(self, selector: Selector("observeEvents"), name: .EKEventStoreChanged, object: eventStore)
}
}
@objc func observeEvents() {
print("called with updated events")
}
}
Am I doing something wrong? What would be the way to subscribe to calendar event updates within the EventStore in SwiftUI?