首先需要導入EventKitUI,如:
進口EventKitUI
並添加委託:EKEventEditViewDelegate
並添加以下代碼來打開新的事件屏幕,傳遞的數據爲:
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event, completion: {(_ granted: Bool, _ error: Error?) -> Void in
if granted {
let event = EKEvent(eventStore: eventStore)
event.title = "My Event Test 1"
//event.location = location;
event.startDate = Date()
event.endDate = event.startDate.addingTimeInterval(60 * 30)
//event.notes = notes;
let controller = EKEventEditViewController()
controller.eventStore = eventStore
controller.event = event
controller.editViewDelegate = self
self.present(controller, animated: true, completion: { _ in })
}
})
:
並根據用戶動作作爲響應處理
func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
switch (action) {
case .canceled:
// User tapped "cancel"
break;
case .saved:
// User tapped "save"
break;
case .deleted:
// User tapped "delete"
break;
}
self.dismiss(animated: true, completion: nil)
}
EventKit會讓從來沒有離開你的應用程序建立在蘋果的日曆事件。 –
@DavidShaw謝謝,但Eventkit不允許在添加事件後進行更改 –