iosswiftswift2ekeventekeventstore

Save event to user's calendar


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: {})
                    }
                })

Solution

  • 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.