Creating an EKCalendar in EKEventStore - Swift

我与影子孤独终老i 提交于 2019-12-04 19:47:01

Try (note this is for a reminder type, but you can just change the type if you need to):

let eventStore = EKEventStore()
var yourReminderCalendar: EKCalendar?

func retrieveYourCalendar() {
    let calendars = eventStore.calendarsForEntityType(EKEntityTypeReminder)

    if(yourReminderCalendar == nil) {
        for calendar in calendars {
            if calendar.title == "Your Title" {
                yourReminderCalendar = (calendar as EKCalendar)
                break
            }
        }

        if(yourReminderCalendar == nil) {
            yourReminderCalendar = EKCalendar(forEntityType: EKEntityTypeReminder, eventStore: eventStore)
            yourReminderCalendar!.title = "Your Title"
            yourReminderCalendar!.source = eventStore.defaultCalendarForNewReminders().source
            var error: NSError?

            if(eventStore.saveCalendar(yourReminderCalendar, commit: true, error: &error)) {
                println("reminder cal saved")
            }
            else {
                //HANDLE REMINDER ERROR
            }
        }
    }
}

You'll want to check for authorization:

    func verifyUserEventAuthorization() {
    switch EKEventStore.authorizationStatusForEntityType(EKEntityTypeReminder) {
    case .Authorized:
        ...
    case .Denied:
        ...
    case .NotDetermined:
        eventStore.requestAccessToEntityType(EKEntityTypeReminder, completion:
            { [weak self](granted: Bool, error: NSError!) -> Void in
                if granted {
                    ...
                }
                else {
                   ...
                }
        })
    case .Restricted:
        ...
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!