func fetchEvent()
{
var store = EKEventStore()
let calendars = store.calendars(for: .event)
for calendar in calendars {
let oneMonthAgo = Date()
let oneMonthAfter = Date(timeIntervalSinceNow: 30*24*3600)
let predicate = store.predicateForEvents(withStart: oneMonthAgo, end: oneMonthAfter, calendars: [calendar])
let events = store.events(matching: predicate)
for event in events {
titles.append(event.title)
startDates.append(event.startDate)
endDates.append(event.endDate)
}
}
[I] use this code to fetch the stored events from the my local calendar. I stored many events in my local calendar but in events always it 0 element. So anyone please help me.
First of all, don't forget to update your Info.plist with a NSCalendarsUsageDescription by adding a new row:
Your ViewController should look similar to this:
You'll first check from Calendar access in
fetchEventsFromCalendar(), and the actual event fetching will be handled infetchEventsFromCalendar().One thing: in this particular example we're looking for events one month before and one month after toady in a calendar called "Calendar". If you want to get the events for that period of time from all the calendars, you can simply remove the
line (including the closing bracket and everything that has to do with
calendarTitle). You'll have an array of all the events.