将事件保存到用户的日历
Posted
技术标签:
【中文标题】将事件保存到用户的日历【英文标题】:Save event to user's calendar 【发布时间】:2017-03-13 00:24:09 【问题描述】:如何将事件添加到用户的日历中,然后允许用户选择日历等。我有这段代码可以工作,但这会将事件添加到用户的默认日历中。如何允许用户更改日历、自定义警报等?我看到其他应用程序打开日历应用程序并预先填写字段。
//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: )
)
【问题讨论】:
【参考方案1】:您可以使用 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 中提供了requested access,等等。
【讨论】:
完美,正是我想要的。日历选项(选择要保存到哪个日历)确实出现在模式视图中。你知道我怎样才能让这个选项可见吗? 我不确定您所说的“使该选项可见”是什么意思(假设您刚刚说出现了日历选项)。如果设备有多个日历,它只会显示为事件的属性。如果用户没有多个日历,则没有。或者您是说您的设备有多个日历,而当事件呈现给用户进行编辑/添加/取消时,您没有看到日历选项? 我的错。我在模拟器上测试。我在设备上测试时很好。【参考方案2】:Apple 没有本机日历 API。
试试这个第三方库Calendar。它看起来与 ios 日历应用程序完全相同,并且还与 EKEventStore 集成。我在我的项目中使用了这个库,没有任何问题。
【讨论】:
【参考方案3】:在 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 键添加到 info plist。
【讨论】:
以上是关于将事件保存到用户的日历的主要内容,如果未能解决你的问题,请参考以下文章
按下 TableViewCell 中的按钮时,将事件从 API 保存到日历