本文介绍了在EKEventStore中创建EKCalendar - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序第一次运行时,我正在尝试在EKEventStore中创建一个日历。我找到了,但我不知道我真的非常了解Objective-C将它翻译成Swift,而且从我已经完成的研究中我也很想知道这种方法在ios8中是否仍然可行。

I'm trying to create a single calendar into EKEventStore when the my app first runs. I found this tutorial but I don't really know enough about Objective-C to translate it to Swift, and also from the research that I've done I'm wondering if this method is still possible in ios8.

推荐答案

尝试(注意这是一个提醒类型,但如果需要,你可以改变类型):

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
            }
        }
    }
}

您需要检查授权:

    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:
        ...
    }
}

这篇关于在EKEventStore中创建EKCalendar - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:19