Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an event in the device calendar using swift?

I would be interested in knowing how to add a calendar event in the device, but using swift. I know there are some examples made in Objective-C, but at the moment nothing in swift. Many thanks.

like image 671
Javier Calatrava Llavería Avatar asked Feb 07 '15 07:02

Javier Calatrava Llavería


People also ask

How do I add events to my iPhone calendar Swift?

event, completion: { (granted, error) in if (granted) && (error == nil) { let event = EKEvent(eventStore: eventStore) event. title = title event. startDate = startDate event. endDate = endDate event.


2 Answers

Note: If your app is crashing with This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCalendarsUsageDescription key with a string value explaining to the user how the app uses this data., you'll need to add NSCalendarsUsageDescription to your info.plist. Can follow the example here.

Swift 5.0 Version

import Foundation import EventKit  let eventStore : EKEventStore = EKEventStore()        // 'EKEntityTypeReminder' or 'EKEntityTypeEvent'  eventStore.requestAccess(to: .event) { (granted, error) in      if (granted) && (error == nil) {       print("granted \(granted)")       print("error \(error)")              let event:EKEvent = EKEvent(eventStore: eventStore)              event.title = "Test Title"       event.startDate = Date()       event.endDate = Date()       event.notes = "This is a note"       event.calendar = eventStore.defaultCalendarForNewEvents       do {           try eventStore.save(event, span: .thisEvent)       } catch let error as NSError {           print("failed to save event with error : \(error)")       }       print("Saved Event")   }   else{          print("failed to save event with error : \(error) or access not granted")   } }    

Reference : https://gist.github.com/mchirico/d072c4e38bda61040f91

like image 166
toofani Avatar answered Sep 24 '22 14:09

toofani


Swift 3.0 compatible:

func addEventToCalendar(title: String, description: String?, startDate: Date, endDate: Date, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {     let eventStore = EKEventStore()      eventStore.requestAccess(to: .event, completion: { (granted, error) in         if (granted) && (error == nil) {             let event = EKEvent(eventStore: eventStore)             event.title = title             event.startDate = startDate             event.endDate = endDate             event.notes = description             event.calendar = eventStore.defaultCalendarForNewEvents             do {                 try eventStore.save(event, span: .thisEvent)             } catch let e as NSError {                 completion?(false, e)                 return             }             completion?(true, nil)         } else {             completion?(false, error as NSError?)         }     }) } 

And also import EventKit

So you can easily call this method from everywhere:

addEventToCalendar(title: "Girlfriend birthday", description: "Remember or die!", startDate: NSDate(), endDate: NSDate()) 

If you prefer, you can put this method inside an utiliy class and define it as 'static'.

like image 33
Luca Davanzo Avatar answered Sep 24 '22 14:09

Luca Davanzo