Why does EKEventStore().requestFullAccessToEvents() work with Simulators but not with real device?
Asked Answered
M

2

5

I'm building an iOS App that creates programatically events in the iOS Calendar. My code works perfectly when run in any iOS Simulator. But then when installled in any physical device, it simply doesn't. What am I doing wrong?

When calling the requestAccess() function, either on the onAppear method, or inside a button, it doesn't work. It seems to be an authentication issue, but no reason why it does work in the simulator and not in physical device.

Has anyone faced the same issue? Any thoughts? Or recommendation?

import Foundation
import EventKit

class EventModel {
    
    let eventStore  = EKEventStore()
    let todayDate : Date = Date()
    
    func requestAccess() {
        let status = EKEventStore.authorizationStatus(for: .event)
        if status == .authorized {
            print("Access is already granted.")
        } else {
            print(status.rawValue)
            eventStore.requestFullAccessToEvents { success, error in
                if success && error == nil {
                    print("Access has been granted.")
                } else {
                    print(error)
                    print("Access request failed with error: \(error?.localizedDescription ?? "Unknown error")")
                }
            }
        }
    }
    
    //more code and other stuff

    }
Milkwhite answered 21/11, 2023 at 7:5 Comment(4)
How do you create and use your EventModel?Montespan
I use it as a variable inside one of the Views.Milkwhite
I'm using SwiftUI to develop this app. Perhaps a Environment Object architecture would help? Would this "eventStore" benefit from the binding characteristics?Milkwhite
Putting it in the environment is probably a good idea. You have to be wary of objects being deallocatedMontespan
R
11

I was able to resolve this same issue in our app - for us the cause was that we hand't updated our info.plist.

We needed to add entry(ies) for the new calendar permissions from iOS 17. Depending on your particular case, you'll need to add one/both of these:

<key>NSCalendarsFullAccessUsageDescription</key>
<string>YOUR DESCRIPTION</string>
<key>NSCalendarsWriteOnlyAccessUsageDescription</key>
<string>YOUR DESCRIPTION</string>

Please note you'll probablt also need to keep the old NSCalendarsUsageDescription entry so that things still work on older versions of iOS.

Hope this helps!

Rabassa answered 22/11, 2023 at 10:51 Comment(2)
It happens when you update Xcode to 15 with the ios17 SDK. Bit me too.Galleywest
This worked! Thank you very much! I had only the the "Privacy - Calendars Usage Description" activated. I needed to add the "Privacy - Calendars Full Access Usage Description" as well.Milkwhite
S
2

We need to add -- latest functions for iOS-17 as it introduces requestFullAccessToEvents.

        if #available(iOS 17.0, *) {
            eventStore.requestFullAccessToEvents(completion: { (granted: Bool, _: Error?) -> Void in
                completion(granted)
            })
        } else {
            // Fallback on earlier versions
            eventStore.requestAccess(to: .event, completion: { (granted: Bool, _: Error?) -> Void in
                completion(granted)
            })
        }

Solution that worked is to add two keys in Info.plist file including previous key i.e. NSCalendarsUsageDescription

<key>NSCalendarsFullAccessUsageDescription</key>
<string>We need this permission in order to set reminders for you</string>
<key>NSCalendarsWriteOnlyAccessUsageDescription</key>
<string>We need this permission in order to set reminders for you</string>
Sheath answered 29/11, 2023 at 8:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.