Klendario is a Swift wrapper over the EventKit
framework. It adds simplicity to the task of managing events in the iOS Calendar by providing handfull functions, extensions and the semi-automatic managment of the user authorization request to access the iOS calendar.
- iOS 9.0+
- Xcode 10.0+
- Swift 4.2+
Add this line to your podfile:
pod 'Klendario'
Add this line to your cartfile
:
github "ThXou/Klendario" ~> 1.0
And then follow the official documentation about Adding frameworks to an application.
Import Klendario
in your source file:
import Klendario
Then set the NSCalendarsUsageDescription
usage description key in your app's Info.plist
file to avoid the Xcode crash on access to sensitive data.
Almost every call will check the user authorization status and returns an error in case the user has not authorized the access to the calendar. If the authorization has not been determined yet, the user is prompted to authorize the application. Anyway you can request authorization manually by calling:
Klendario.requestAuthorization { (granted, status, error) in
if let error = error {
print("error: \(error.localizedDescription)")
} else {
print("authorization granted!")
}
}
The closure will return an error if the user has denied the access or if the access is restricted due to, for example, parental controls.
If you prefer, you can check if the user has granted access to the calendar using:
Klendario.isAuthorized()
Create an event is as easy as this:
let event = Klendario.newEvent()
event.title = "Awesome event"
event.startDate = Date()
event.endDate = Date().addingTimeInterval(60*60*2) // 2 hours
event.save()
If you have the specific calendar where you want to add the event, you can pass it in the newEvent()
function as a parameter. If you additionaly want to perform some actions on saving, you can use the optional completion closure:
...
event.save { error in
if let error = error {
print("error: \(error.localizedDescription)")
} else {
print("event successfully created!")
}
}
Before saving events read the Last but not least section.
You can get an event by knowing its event identifier:
Klendario.getEvent(with: eventIdentifier) { (event, error) in
guard let event = event else { return }
print("got an event: \(event.title ?? "")")
}
Or you can get a group of events between two dates and in specific calendars:
Klendario.getEvents(from: Date(),
to: Date() + 60*60*2,
in: calendars) { (events, error) in
guard let events = events else { return }
print("got \(events.count) events")
}
You can easily delete an EKEvent
object using the delete
function:
event.delete()
As for creating an event, you can perform actions on event deletion completion. You can omit any paremeter:
event.delete(span: .futureEvents, commit: false) { error in
if let error = error {
print("error: \(error.localizedDescription)")
} else {
print("event successfully deleted!")
}
}
Before deleting events read the Last but not least section.
Create a new calendar is as easy as create a new event:
let calendar = Klendario.newCalendar()
calendar.title = "Awesome calendar"
calendar.save()
As for events, you can use a completion
closure to handle possible errors or perform some action on task completion. This method is flexible so you can pass a different eventStore
or source
if you don't want to use the default one.
Before saving calendars read the Last but not least section.
To get all the iOS calendars which includes events in the device, simply call:
let calendars = Klendario.getCalendars()
You can easily delete an EKCalendar
object using the delete
function:
calendar.delete()
As for creating an event, you can perform actions on event deletion completion. You can omit any paremeter:
calendar.delete(commit: true) { error in
if let error = error {
print("error: \(error.localizedDescription)")
} else {
print("calendar successfully deleted!")
}
}
Before deleting calendars read the Last but not least section.
It is very simple to save or delete an event or a calendar with Klendario
, you just need to call save()
or delete()
on the object as shown in previous sections. Be carefull to not call save()
or delete()
on objects not retrieved with the Klendario
API, because it shares the same EKEventStore
object across all the API calls.
EventKit
does not support cross-store saving at least until iOS 12.