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

beginner_T picture beginner_T · Nov 9, 2015 · Viewed 17.1k times · Source

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
}

Answer

zcui93 picture zcui93 · Nov 9, 2015

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