Search code examples
iphoneswiftios12

EKCalendar title in iOS12 is blank


In iOS 12 I'm trying to list the calendars. I can print the calendar ids, but the titles are all blank. What am I doing wrong?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let status = EKEventStore.authorizationStatus(for: EKEntityType.event)

        switch (status) {
        case EKAuthorizationStatus.notDetermined:
            EKEventStore().requestAccess(to: .event, completion: {
                (granted: Bool, error: Error?) in

                if granted != true {
                    print("Access not granted")
                }
            })
        case EKAuthorizationStatus.authorized:
            print("Access granted")
        case EKAuthorizationStatus.restricted, EKAuthorizationStatus.denied:
            print("Access restricted or denied")
        }

        print("Calendars:")
        for c in EKEventStore().calendars(for: EKEntityType.event) {
            print("    \(c.calendarIdentifier):\(c.title)")
        }
    }

Solution

  • I'm not sure why, but this code shows event titles in iOS 12 Simulator.

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let status = EKEventStore.authorizationStatus(for: EKEntityType.event)
        let eventStore = EKEventStore() //<-
        switch (status) {
        case EKAuthorizationStatus.notDetermined:
            eventStore.requestAccess(to: .event, completion: {
                (granted: Bool, error: Error?) in
    
                if granted != true {
                    print("Access not granted")
                }
            })
        case EKAuthorizationStatus.authorized:
            print("Access granted")
        case EKAuthorizationStatus.restricted, EKAuthorizationStatus.denied:
            print("Access restricted or denied")
        }
    
        print("Calendars:")
        for c in eventStore.calendars(for: EKEntityType.event) {
            print("    \(c.calendarIdentifier):\(c.title)",c)
        }
    }
    

    Maybe, you need to keep strong reference to the instance of EKEventStore while accessing EKCalendar properties.