Swift under macOS- How to read Calendar events access problems

143 Views Asked by At

I did create this part of the code to import the Agenda event, but it does not read any event. Did set in the SandBox settings the Agenda checkbox, did check the entitlements file. Did insert a Calendar usage description in the NSCalendarsUsageDescription string. Also to be sure the ‘ Privacy - Calendars Usage Description’ with a description. I never get the user when the app prompted for a permission to use the calendar. With the first call to check the access code I get a 'Denied' and the second time I get 'notDetermined'. I have a valid Developer ID with a Paid subscription. Did I miss something important here or did I write the wrong code?

 struct CalendarView: View {
     let eventStore = EKEventStore()
     @State private var events: [EKEvent] = []
     @State private var result: String = ""
     @State private var eventResult: String = ""
    
     var body: some View {
        VStack{
            Text(result)
            Text(eventResult)
            List(events, id: \.self) { event in
                Text(event.title)
            }
            .onAppear {
                requestAccess()
            }
            Spacer()
        }
     }
    
     private func checkStatusAndGetAllEvents() -> String {
        var eventResult: String = "What?"
        let currentStatus = EKEventStore.authorizationStatus(for: EKEntityType.event)
        
        switch currentStatus {
        case .authorized:
            eventResult = "authorized"
        case .notDetermined:
            eventResult = "notDetermined"
            eventStore.requestAccess(to: .event) { accessGranted, error in
                if accessGranted {
                    eventResult = "notDetermined not granted"
                } else {
                    eventResult = "Change Settings to Allow Access"
                }
            }
        case .restricted:
            eventResult = "restricted"
        case .denied:
            eventResult = "denied"
        @unknown default:
            eventResult = "default"
        }
        return eventResult
     }
    
     private func requestAccess() {
        let secondsADay = 86.400
        eventResult = checkStatusAndGetAllEvents()
        eventStore.requestAccess(to: .event) { granted, error in
            if granted {
                result = "granted"
                let endDate = Date().addingTimeInterval( secondsADay * 7)
                let predicate = eventStore.predicateForEvents(withStart: Date(), end: endDate, calendars: nil)
                events = eventStore.events(matching: predicate)
            } else {
                // Handle access denied error
                result = checkStatusAndGetAllEvents()
            }
        }
       }
       }

What am I supposed to do with the setting or code to get it to read the events correctly? Anyone with suggestions?

0

There are 0 best solutions below