skip to Main Content

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

    }

2

Answers


  1. 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!

    Login or Signup to reply.
  2. We dont 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search