I would like to delete an EKCalendar. This works fine on iOS devices and simulators, however trying to get this working on Catalina is problematic. Whatever I try I get this:
Error Domain=EKErrorDomain Code=72 "Can't delete a calendar if doing so would leave the account with no calendars which are eligible to be the default scheduling calendar." UserInfo={NSLocalizedDescription=Can't delete a calendar if doing so would leave the account with no calendars which are eligible to be the default scheduling calendar.}
Any pointers? I have been chasing this for weeks! Thanks!
I have been granted permission for both Calendars and Reminders:
import UIKit
import EventKit
class ViewController: UIViewController {
let eventStore = EKEventStore()
func deleteCal (eventStoreToUse: EKEventStore) {
let calsArray = eventStoreToUse.calendars(for: .event)
for cals in calsArray {
print (cals.title)
if cals.title == "Gaps2" || cals.title == "Done" {
do { try _ = eventStoreToUse.removeCalendar(cals, commit: true)
} catch {
print ("Error \(error)")
}
} else {
print ("Did not delete \(cals.title)")
}
}
}
func askAccess() {
switch EKEventStore.authorizationStatus(for: .event) {
case .authorized:
print ("Calendars Access Granted")
case .denied:
print("Access denied")
case .notDetermined:
eventStore.requestAccess(to: .event, completion:
{[weak self] (granted: Bool, error: Error?) -> Void in
if granted {
print("Granted")
self?.deleteCal(eventStoreToUse: (self?.eventStore)!)
} else {
print("Access denied")
}
})
default:
print("Case default")
}
switch EKEventStore.authorizationStatus(for: .reminder) {
case .authorized:
print ("Reminders Access Granted")
case .denied:
print("Access denied")
case .notDetermined:
eventStore.requestAccess(to: .event, completion:
{[weak self] (granted: Bool, error: Error?) -> Void in
if granted {
print("Granted")
self?.deleteCal(eventStoreToUse: (self?.eventStore)!)
} else {
print("Access denied")
}
})
default:
print("Case default")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
askAccess()
deleteCal(eventStoreToUse: eventStore)
}
}