1

I want to add a new event to the IOS calendar, I know this can be done easily using the EventKit. However I do not want to add the event directly, I want the calendar app to be launched with the "add event" screen pre-filled with data passed from my application, as shown in the attached screenshot, and the user can have the option to edit the event details, save the event or cancel it. I know I can launch the calendar app using the "calshow:" NSURL and UIApplication.shared.open function, however this only opens the day I pass it and not the "add event" screen I would like to open.

I've searched high and low for a way to do it but found nothing.

Any suggestions?

add event screen to be opened when calendar is launched

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
HannahSaud
  • 33
  • 5

1 Answers1

2

Yes, You can use EventKitUI framework for that.

Use EKEventEditViewController.

Presented modally, the event edit view controller provides a way for users to add new events, as well as edit or delete events from their calendar.

Ref : Apple

For pre-filled values use event property.

// Create event 
   var store = EKEventStore()
   var event = EKEvent(eventStore: store)
   event.title = "Title"
        //event.startDate = startDate
        //event.endDate = currentEvent?.endDate


  let eventViewController: EKEventEditViewController = EKEventEditViewController()
  eventViewController.event = event
  eventViewController.eventStore = store
  eventViewController.editViewDelegate = self

present(eventViewController, animated: true, completion: nil)
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • I'm gettin an error: "Use of undeclared type 'EKEventEditViewController'" even though I've imported the EventKit library – HannahSaud Jul 25 '19 at 10:25
  • Just imported the EventKitUI library and the error is gone, however I'm gettin another error that says: "Cannot call value of non-function type 'EKEventEditViewController'" – HannahSaud Jul 25 '19 at 10:31
  • I just realized this is because my class is not of type UIViewController, I changed it ti that and was able to build, however it did not present the eventViewController – HannahSaud Jul 25 '19 at 10:49
  • @HannahSaud: Present it from your view controller. updated answer – Toseef Khilji Jul 25 '19 at 10:54
  • Thank you, it's working now. However I cannot edit the date and time, and the add and cancel buttons do nothing. Do I have to handle them manually? if so how do I do that? I checked the functions in EKEventEditViewController but didn't find any related to allowing the editing of dates or adding actions to the cancel and Add buttons. – HannahSaud Jul 25 '19 at 11:16
  • I did set it to self. The cancel and edit buttons are still not working – HannahSaud Jul 28 '19 at 05:01
  • Got the solution, my class wasn't of type UIViewController so I changed it to that and added this extension: extension CalendarEvent: EKEventEditViewDelegate { func eventEditViewController(controller: EKEventEditViewController, didCompleteWithAction action: EKEventEditViewAction) { controller.dismiss(animated: true, completion: nil) } } It's working now, thank you for all your help :) – HannahSaud Jul 28 '19 at 05:39