Swift 4 How to get all events from calendar?
In case anyone is still wondering, Apple limited this to four years intentionally.
From predicateForEvents(withStart:end:calendars:):
For performance reasons, this method matches only those events within a four year time span. If the date range between startDate and endDate is greater than four years, it is shortened to the first four years.
If you want a range longer than that, I guess you'll have to wrap this in your own function to split it into four year chunks.
How to delete all events from the default calendar
There is no removeAll
method.
You need to use the methods of EKEventStore
to query and remove the desired events.
At a high level you need to:
- Request authorization to access calendar events.
- Get a reference to desired the
EKCalendar
. - Create a predicate for the events you wish to query. You need a date range and the calendar.
- Enumerate the events that match the predicate.
- Delete each enumerated event.
All of the needed APIs are in the EKEventStore
class. See its documentation for specifics.
How to fetch my app's events from calendar?
func getEvents() -> [EKEvent] {
var allEvents: [EKEvent] = []
// calendars
let calendars = self.eventStore.calendars(for: .event)
// iterate over all selected calendars
for (_, calendar) in calendars.enumerated() where isCalendarSelected(calendar.calendarIdentifier) {
// predicate for today (start to end)
let predicate = self.eventStore.predicateForEvents(withStart: self.initialDates.first!, end: self.initialDates.last!, calendars: [calendar])
let matchingEvents = self.eventStore.events(matching: predicate)
// iterate through events
for event in matchingEvents {
allEvents.append(event)
}
}
return allEvents
}
this code will gvie you all events and then you have to filter it by title or something which can be identify that this event is from your app :)
Swift 4.2 EventKit get all Events to the selected calendar on TableView didSelectedRowAt
You have to pass the calendar in the calendars
parameter of predicateForEvents
let rowCalendar = calendars![indexPath.row]
let calendarTitle = rowCalendar.title
print(calendarTitle)
...
if let anAgo = oneDayAgo, let aNow = oneDayFromNow {
predicate = eventStore.predicateForEvents(withStart: anAgo, end: aNow, calendars: [rowCalendar])
}
And please declare the data source array as non-optional empty array as you are force unwrapping it anyway
var calendars = [EKCalendar]()
Related Topics
Ruby: Loaderror - Library Not Found for Class Digest::Sha1 -- Digest/Sha1
Opening Viewcontroller in Appdelegate While Keeping Tabbar
Replace C Style For-Loop in Swift 2.2.1
Uitraitcollection Clarification
How to Find the Index of a Tuple Element from an Tuple Array? iOS, Swift
Swift Function That Takes in Array Giving Error: '@Lvalue $T24' Is Not Identical to 'Cgfloat'
Swift Parse - Local Datastore and Displaying Objects in a Tableview
Add View as a Parameter to a Custom Viewmodifier
iOS 8: Autorotation Is Not Working Without Storyboard
Xcode 9.3 - Nspredicate Bool Crash
Video Thumbnail Taking 10-15 Secs to Display
Mirror Not Working in Swift When Iterating Through Children of an Objective-C Object
Animating Strings Fading In/Out in Swift
Uibezierpath + Cashapelayer - Animate a Circle Filling Up
Realmswift Cannot Cast Results<Someojbect> to Results<Object>
Dynamically Passing Closure with Keypaths to a Sorting Function
Can't Dismiss Navigation Controller in Swift
Which View Controller Should I Pass in My Swift Native Code for a Flutter Plugin