尝试(请注意,这是一种提醒类型,但如果需要,您可以更改类型):
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:
...
}
}