swift2 将活动保存到用户日历

nwo49xxi  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(213)

如何将事件添加到用户的日历,然后允许用户选择日历等。我有这样的代码,它可以工作,但这将事件添加到用户的默认日历。我如何允许用户更改日历,自定义提醒等?我看到其他应用程序打开日历应用程序,并预先填写字段。

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

hm2xizp91#

您可以使用Apple的原生日历API。在EventKitUI framework中使用EKEventEditViewController,用户将能够在保存事件时指定日历。在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)
    }
}

在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)
    }
}

这假设你已经在Info.plist中提供了一个NSCalendarsUsageDescription,你已经有了requested access,等等。

piok6c0g

piok6c0g2#

Apple没有本地日历API。
试试这个第三方库Calendar。它看起来和iOS calandar应用程序完全一样,而且它也集成了EKEventStore。我在我的项目中使用过这个库,没有任何问题。

7uzetpgm

7uzetpgm3#

在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)

            }
        }

       })
    }

}

现在将出现以下屏幕,您还可以在此处根据要求自定义详细信息:

  • //现在在添加事件后关闭视图控制器 *
extension yourViewController: EKEventEditViewDelegate {

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

    }
}

注意:不要忘记将NSCalendarsUsageDescription项添加到信息列表中。

相关问题