Empty titles in calendars while importing

344 Views Asked by At

I have code like:

let calendars = EKEventStore().calendars(for: entityType)  

for calendar in calendars {  
    ...  
    createListFrom(calendar: calendar, entityType: entityType)  
}  

...  

newList.name = calendar.title  

In iOS 10, calendar.title is real name, but in iOS 11 it is always nil.

Is it an iOS 11 bug or am I doing something wrong?

My Problem in this line

let calendars = EKEventStore().calendars(for: entityType)  

But it works that way

let eventStore = EKEventStore()

let calendars = eventStore.calendars(for: .event)
2

There are 2 best solutions below

2
On

For calendar use the below lines of code.

var calenders: [EKCalendar]?
let eventStore = EKEventStore();

func fetchCalendarEvents(){
 calenders = eventStore.calendars(for: .event)
 for calendar in calenders!{
   print(calendar.title)
 }
}
0
On

I had same problem that same code work in iOS 10 perfectly but in iOS 11 not work

the problem become from permission that you request from object and you try to get info from other

you must request for permission every time you need to init your calendar object and use this object to get your info

let eventStore = EKEventStore()
        eventStore.requestAccess(to: .event, completion: {
            (accessGranted: Bool, error: Error?) in

            if accessGranted == true {

                let calendars = eventStore.calendars(for: entityType)  

                for calendar in calendars {  
                ...  
                createListFrom(calendar: calendar, entityType: entityType)  
                }  
            }
        }