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
}
The Nylas API For Apple Calendar Provides Application Access for All Users.
To create a calendar UI in our Swift application, we'll start with DatePicker from SwiftUI. This structure is mainly used to select dates from a user interface, and it is very easy to use and control. Let's add DatePicker into our view body. Add the code below to the ContentView.
You can try something like this
import EventKit
var titles : [String] = []
var startDates : [Date] = []
var endDates : [Date] = []
var store = EKEventStore()
let calendars = store.calendars(for: .event)
for calendar in calendars {
if calendar.title == "Work" {
let oneMonthAgo = Date(timeIntervalSinceNow: -30*24*3600)
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)
}
}
}
(Updated with Swift 5 APIs)
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.
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)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With