How to get all Events out of a Calendar (Swift)

2019-03-31 06:47发布

问题:

I am kinda new to Swift, but really interested. I would like to get all the events I stored in a Calendar called "Work" and show them in a tableView. I was looking for questions like this, but the code shown there seems to be kinda old and not really working. How do I do that? The tableView should be able to show the Title, start and end-Date. Is it possible to get like all Titles in an Array of Strings. Same with the start and end? Would be awesome to get some tips!

Update: I declared the variables outside the class. Now I tried a code that looks like this, thanks to an answer I got here, but I don't get the cells to display anything?! And Yes I already created a testEvent in my Work calendar on the simulator.

    override func viewDidAppear(animated: Bool) {

    let eventStore = EKEventStore()

    switch EKEventStore.authorizationStatusForEntityType(.Event) {
    case .Authorized:
        readEvents()
    case .Denied:
        print("Access denied")
    case .NotDetermined:

        eventStore.requestAccessToEntityType(.Event, completion: { (granted: Bool, NSError) -> Void in
            if granted {
                self.readEvents()

            }else{
                print("Access denied")
            }



        })
    default:
        print("Case Default")
    }
    self.tableView.reloadData()
}



func readEvents() {



    let eventStore = EKEventStore()
    let calendars = eventStore.calendarsForEntityType(.Event)

    for calendar in calendars {
        if calendar.source.title == "Work" {
            let oneMonthAgo = NSDate(timeIntervalSinceNow: -30*24*3600)
            let oneMonthAfter = NSDate(timeIntervalSinceNow: +30*24*3600)


            let predicate = eventStore.predicateForEventsWithStartDate(oneMonthAgo, endDate: oneMonthAfter, calendars: [calendar])

            var events = eventStore.eventsMatchingPredicate(predicate)

            for event in events {

                titles.append(event.title)
                startDates.append(event.startDate)
                endDates.append(event.endDate)


            }

        }
    }


}

// MARK: - Table view data source



override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return titles.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)


    cell.textLabel!.text = titles[indexPath.row]
    cell.detailTextLabel!.text = "From: \(startDates[indexPath.row]) Until: \(endDates[indexPath.row])"
    // Configure the cell...

    return cell
}

回答1:

You can try something like this

    import EventKit

    var titles : [String] = []
    var startDates : [NSDate] = []
    var endDates : [NSDate] = []

    let eventStore = EKEventStore()
    let calendars = eventStore.calendarsForEntityType(.Event)

    for calendar in calendars {
        if calendar.title == "Work" {

            let oneMonthAgo = NSDate(timeIntervalSinceNow: -30*24*3600)
            let oneMonthAfter = NSDate(timeIntervalSinceNow: +30*24*3600)

            let predicate = eventStore.predicateForEventsWithStartDate(oneMonthAgo, endDate: oneMonthAfter, calendars: [calendar])

            var events = eventStore.eventsMatchingPredicate(predicate)

            for event in events {
                titles.append(event.title)
                startDates.append(event.startDate)
                endDates.append(event.endDate)
            }
        }
    }


回答2:

Swift 3.0

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

for calendar in calendars {
    if calendar.title == "Work" {

        let oneMonthAgo = NSDate(timeIntervalSinceNow: -30*24*3600)
        let oneMonthAfter = NSDate(timeIntervalSinceNow: +30*24*3600)

        let predicate = eventStore.predicateForEvents(withStart: oneMonthAgo as Date, end: oneMonthAfter as Date, calendars: [calendar])

        let events = eventStore.events(matching: predicate)

        for event in events {
            titles.append(event.title)
            startDates.append(event.startDate as NSDate)
            endDates.append(event.endDate as NSDate)
        }
    }
}


回答3:

func fetchEventsFromCalendar() -> Void {
    let status = EKEventStore.authorizationStatus(for: EKEntityType.event)

    switch (status) {
    case .notDetermined:
        requestAccessToCalendar()
    case .authorized:
        self.fetchEventsFromCalendar(calendarTitle: "Calendar")
        break
    case .restricted, .denied: break

    }
}


func requestAccessToCalendar() {

    eventStore.requestAccess(to: EKEntityType.event) { (accessGranted, error) in

        self.fetchEventsFromCalendar(calendarTitle: "Calendar")

    }

}


// MARK: Fetech Events from Calendar
func fetchEventsFromCalendar(calendarTitle: String) -> Void {

    //PGAEventsCalendar
    for calendar:EKCalendar in calendars! {

        if calendar.title == calendarTitle {

        let selectedCalendar = calendar
        let startDate = NSDate(timeIntervalSinceNow: -60*60*24*180)
        let endDate = NSDate(timeIntervalSinceNow: 60*60*24*180)
        let predicate = eventStore.predicateForEvents(withStart: startDate as Date, end: endDate as Date, calendars: [selectedCalendar])
        addedEvents = eventStore.events(matching: predicate) as [EKEvent]

        print("addedEvents : \(addedEvents)")

        }
    }

}

Make sure to give the proper title of the calendar, because different calendars have different list of events, In my case calendar with title "Calendar" was having the required events list, before giving check for this :

if calendar.title == calendarTitle {

My first list was getting overrides by list from other calendar, so I used to get only few events which were not relevant to me.