Home > Software design >  How to fetch the events from local calendar in iOS swift
How to fetch the events from local calendar in iOS swift

Time:08-12

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)
         }
}

use this code to fetch the stored events from the my local calender . I stored many events in my local calender but in events always it 0 element . so anyone please help me

CodePudding user response:

First of all, don't forget to update your Info.plist with a NSCalendarsUsageDescription by adding a new row:

  • Key: "Privacy - Calendars Usage Description"
  • Value: "Please give access" - this is up to you

Your ViewController should look similar to this:

import UIKit
import EventKit

final class ViewController: UIViewController {
    let eventStore = EKEventStore()
    var titles: [String] = []
    var startDates: [Date] = []
    var endDates: [Date] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        fetchEventsFromCalendar()
    }

    func fetchEventsFromCalendar() -> Void {
        let status = EKEventStore.authorizationStatus(for: EKEntityType.event)
        switch status {
        case .notDetermined: requestAccessToCalendar("Calendar")
        case .authorized: fetchEventsFromCalendar("Calendar")
        case .denied: print("Access denied")
        default: break
        }
    }

    func requestAccessToCalendar(_ calendarTitle: String) {
        eventStore.requestAccess(to: EKEntityType.event) { (_, _) in
            self.fetchEventsFromCalendar(calendarTitle)
        }
    }

    func fetchEventsFromCalendar(_ calendarTitle: String) -> Void {
        for calendar in eventStore.calendars(for: .event) {
            if calendar.title == calendarTitle {
                let oneMonthAgo = Calendar.current.date(byAdding: .month, value: -1, to: Date()) ?? Date()
                let oneMonthAfter = Calendar.current.date(byAdding: .month, value: 1, to: Date()) ?? Date()
                let predicate = eventStore.predicateForEvents(
                    withStart: oneMonthAgo,
                    end: oneMonthAfter,
                    calendars: [calendar]
                )
                let events = eventStore.events(matching: predicate)
                for event in events {
                    titles.append(event.title)
                    startDates.append(event.startDate as Date)
                    endDates.append(event.endDate as Date)
                }
            }
        }

        // Print the event titles so check if everything works correctly
        print(titles)
    }

}

You'll first check from Calendar access in fetchEventsFromCalendar(), and the actual event fetching will be handled in fetchEventsFromCalendar().

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

            if calendar.title == calendarTitle {

line (including the closing bracket and everything that has to do with calendarTitle). You'll have an array of all the events.

  • Related