将事件保存到用户的日历 [英] Save event to user's calendar
问题描述
如何将事件添加到用户的日历中,然后允许用户选择日历等。我有此代码可用,但这会将事件添加到用户的默认日历中。如何允许用户更改日历,自定义警报等?我见过其他应用程序打开日历应用程序并预先填写字段。
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: {})
}
})
推荐答案
您可以使用Apple的原生日历API。在 EKEventEditViewController
href =https://developer.apple.com/reference/eventkitui =noreferrer> EventKitUI框架,用户可以在保存事件时指定日历。在Swift 3中:
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)
}
}
在Swift 2.3中:
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)
}
}
这假设您在 Info.plist
NSCalendarsUsageDescription >,您已请求的访问权限等。
This assumes that you've supplied a NSCalendarsUsageDescription
in your Info.plist
, that you've requested access, etc.
这篇关于将事件保存到用户的日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!