9

How do you add an event to the user's calendar, but then allow the user to choose the calendar, etc. I have this code that works, but this adds the event to the user's default calendar. How do I allow the user to change the calendar, customize the alerts etc? I have seen other apps open the calendar app and pre-fill the fields.

//add to calendar
                let eventStore : EKEventStore = EKEventStore()
                eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) in
                    if granted && error == nil {
                        let event:EKEvent = EKEvent(eventStore: eventStore)

                        event.title = "My event: " + self.event.name
                        event.startDate = self.event.startTime
                        event.endDate = self.event.endTime
                        event.notes = self.event.description
                        event.calendar = eventStore.defaultCalendarForNewEvents

                        do {
                            try eventStore.saveEvent(event, span: .ThisEvent, commit: true)
                            self.dismissViewControllerAnimated(true, completion: {})
                        } catch {
                            self.dismissViewControllerAnimated(true, completion: {})
                        }
                    } else {
                        self.dismissViewControllerAnimated(true, completion: {})
                    }
                })
Prabhu
  • 12,995
  • 33
  • 127
  • 210

3 Answers3

14

You can use Apple's native calendar API. Use EKEventEditViewController in the EventKitUI framework, and the user will be able to specify the calendar when saving the event. In Swift 3:

import UIKit
import EventKit
import EventKitUI

class ViewController: UIViewController {

    let store = EKEventStore()

    func createEvent() {
        // create the event object

        let event = EKEvent(eventStore: store)
        event.title = "Foo"
        event.startDate = ...
        event.endDate = ...

        // prompt user to add event (to whatever calendar they want)

        let controller = EKEventEditViewController()
        controller.event = event
        controller.eventStore = store
        controller.editViewDelegate = self
        present(controller, animated: true)
    }
}

extension ViewController: EKEventEditViewDelegate {

    func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
        controller.dismiss(animated: true)
    }
}

In Swift 2.3:

import UIKit
import EventKit
import EventKitUI

class ViewController: UIViewController {

    let store = EKEventStore()

    func createEvent() {
        // create the event object

        let event = EKEvent(eventStore: store)
        event.title = "Foo"
        event.startDate = ...
        event.endDate = ...

        // prompt user to add event (to whatever calendar they want)

        let controller = EKEventEditViewController()
        controller.event = event
        controller.eventStore = store
        controller.editViewDelegate = self
        presentViewController(controller, animated: true, completion: nil)
    }
}

extension ViewController: EKEventEditViewDelegate {

    func eventEditViewController(controller: EKEventEditViewController, didCompleteWithAction action: EKEventEditViewAction) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
}

This assumes that you've supplied a NSCalendarsUsageDescription in your Info.plist, that you've requested access, etc.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Perfect, exactly what I was looking for. The calendar option (to choose which calendar to save to) does appear in the modal view though. Do you know how I could make that option visible? – Prabhu Mar 13 '17 at 06:28
  • I'm not sure what you mean by "make that option visible" (given that you just said the calendar option appeared). If device has multiple calendars, it just shows up as attribute of the event. If user doesn't have multiple calendars, it doesn't. Or are you saying that you have a device where there are multiple calendars and you're not seeing the calendar option when the event is presented to the user for edit/adding/canceling? – Rob Mar 13 '17 at 06:37
  • 1
    My bad. I was testing on the simulator. It's fine when I tested on device. – Prabhu Mar 13 '17 at 06:42
0

Apple doesn't have the native calendar API.

Try this third-party library Calendar. It looks exactly the same as the iOS calandar app and it is integrated with EKEventStore as well. I have used this library in my project without any problem.

The Mach System
  • 6,703
  • 3
  • 16
  • 20
0

Working perfectly in swift 4.2

import UIKit
import EventKit
import EventKitUI

class yourViewController: UIViewController{

    let eventStore = EKEventStore()

    func addEventToCalendar() {

    eventStore.requestAccess( to: EKEntityType.event, completion:{(granted, error) in
        DispatchQueue.main.async {
            if (granted) && (error == nil) {
                let event = EKEvent(eventStore: self.eventStore)
                event.title = self.headerDescription
                event.startDate = self.parse(self.requestDetails.value(forKey: "session_time") as? String ?? "")
                event.endDate = self.parse(self.requestDetails.value(forKey: "session_end_time") as? String ?? "")
                let eventController = EKEventEditViewController()
                eventController.event = event
                eventController.eventStore = self.eventStore
                eventController.editViewDelegate = self
                self.present(eventController, animated: true, completion: nil)

            }
        }


       })
    }

}

Now below screen will appear and here you can also customize details as per requirement:

enter image description here

// Now dismiss view controller after adding your event

extension yourViewController: EKEventEditViewDelegate {

    func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
        controller.dismiss(animated: true, completion: nil)

    }
}

Note: Don't forget to add NSCalendarsUsageDescription key into info plist.

Alok
  • 24,880
  • 6
  • 40
  • 67