一尘不染

如何使用Swift在设备日历中添加事件

swift

我想知道如何在设备中添加日历事件,但是要使用swift。我知道在Objective-C中有一些示例,但是目前还没有很快。非常感谢。


阅读 220

收藏
2020-07-07

共1个答案

一尘不染

Swift 3.0版本

    let eventStore : EKEventStore = EKEventStore()

    // 'EKEntityTypeReminder' or 'EKEntityTypeEvent'

    eventStore.requestAccess(to: .event) { (granted, error) in

        if (granted) && (error == nil) {
            print("granted \(granted)")
            print("error \(error)")

            let event:EKEvent = EKEvent(eventStore: eventStore)

            event.title = "Test Title"
            event.startDate = Date()
            event.endDate = Date()
            event.notes = "This is a note"
            event.calendar = eventStore.defaultCalendarForNewEvents
            do {
                try eventStore.save(event, span: .thisEvent)
            } catch let error as NSError {
                print("failed to save event with error : \(error)")
            }
            print("Saved Event")
        }
        else{

            print("failed to save event with error : \(error) or access not granted")
        }
    }

参考:https://gist.github.com/mchirico/d072c4e38bda61040f91

2020-07-07