将事件保存到用户的日历
Save event to user's calendar
如何将事件添加到用户的日历,然后允许用户选择日历等。我有这段有效的代码,但这会将事件添加到用户的默认日历。我如何允许用户更改日历、自定义警报等?我已经看到其他应用程序打开日历应用程序并预填字段。
//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: {})
}
})
Apple 没有原生日历 API。
试试这个第三方库 Calendar。它看起来与 iOS 日历应用程序完全相同,并且也与 EKEventStore 集成。我在我的项目中使用这个库没有任何问题。
您可以使用 Apple 的原生日历 API。使用EKEventEditViewController
in the EventKitUI framework,用户可以在保存事件时指定日历。在 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,等等
在 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 键添加到信息 plist 中。
如何将事件添加到用户的日历,然后允许用户选择日历等。我有这段有效的代码,但这会将事件添加到用户的默认日历。我如何允许用户更改日历、自定义警报等?我已经看到其他应用程序打开日历应用程序并预填字段。
//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: {})
}
})
Apple 没有原生日历 API。
试试这个第三方库 Calendar。它看起来与 iOS 日历应用程序完全相同,并且也与 EKEventStore 集成。我在我的项目中使用这个库没有任何问题。
您可以使用 Apple 的原生日历 API。使用EKEventEditViewController
in the EventKitUI framework,用户可以在保存事件时指定日历。在 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,等等
在 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 键添加到信息 plist 中。