Save event to user’s calendar

i0S Swift Issue

Question or problem in the Swift programming language:

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

How to solve the problem:

Solution 1:

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.

Solution 2:

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.

Solution 3:

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.

Hope this helps!