Error Getting Default Calendar For New Events - Sw

2019-07-04 03:19发布

问题:

I encountered an issue trying to create a new event on iOS's Calendar application while using Swift.

This is what I have so far:

func addToCal(){
    let eventStore = EKEventStore()

    eventStore.requestAccessToEntityType(EKEntityTypeReminder) {
        (granted: Bool, err: NSError!) in
        if granted && !err {
            var event:EKEvent = EKEvent(eventStore: eventStore)
            event.title = self.eventTitle!.text
            event.startDate = self.eventData.startDateObj
            event.endDate = self.eventData.endDateObj
            event.calendar = eventStore.defaultCalendarForNewEvents
            eventStore.saveEvent(event, span: EKSpanThisEvent, error: nil)
            println("Saved Event")
        }
    }

This is the error that I'm getting:

Error getting default calendar for new events: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"

I checked my syntax and I'm pretty sure I got it right, so can anyone help me figure out where I'm going wrong?

Additional Information

  • iOS 8 Beta 5
  • Xcode 6 Beta 5
  • Issue occurs on iPad Mini w/ Retina Display (real device)

Update

Changing EKEntityTypeReminder to EKEntityTypeEvent (Sorry, my mistake) actually doesn't produce an error, but now the event doesn't even show up in Calendar. I checked the outputs of granted and err and I see false and nil, respectively.

回答1:

I think your main problem was that you weren't committing the new event to be saved. The following code is what I use, and the prime difference is that my code includes a commit and allows my error to be changed from 'nil' if there is one. The conditional at the end just prints any reports of save errors or save successes to the Debug Area.

let eventStore = EKEventStore()
let event = EKEvent(eventStore: eventStore)

event.title = "Your Event Title Here" // Sets event's title
event.startDate = NSDate() // Sets event's start date
event.endDate = event.startDate.dateByAddingTimeInterval(20000) // Sets event's end date
event.calendar = eventStore.defaultCalendarForNewEvents // Selects default calendar

var saveError : NSError? = nil // Initially sets errors to nil
eventStore.saveEvent(event, span: EKSpanThisEvent, commit: true, error: &saveError) // Commits changes and allows saveEvent to change error from nil 

//// Following checks for errors and prints result to Debug Area ////
if saveError != nil {
    println("Saving event to Calendar failed with error: \(saveError!)")
} else {
    println("Successfully saved '\(event.title)' to '\(event.calendar.title)' calendar.")
}

Hope this helps!

-Gorowski