Save event to user's calendar

与世无争的帅哥 提交于 2019-12-01 15:48:11

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.

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.

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:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!