ebarooni / capacitor-calendar

The Capacitor Calendar Plugin enables full calendar functionality on iOS and Android, with added reminder support for iOS devices.
https://capacitor-calendar.pages.dev
MIT License
18 stars 5 forks source link

Error creating new calendar #105

Open iamandreadompe opened 2 weeks ago

iamandreadompe commented 2 weeks ago

Hello to everyone!

i'm trying to create a new calendar on my iPhone

i do this

const permissionResults = await CapacitorCalendar.requestAllPermissions();

Permission results {"readCalendar":"granted","writeReminders":"granted","readReminders":"granted","writeCalendar":"granted"}

    async createCalendar() {
        try {
            const response: any = await CapacitorCalendar.createCalendar({
                title: this.newCalendarName,
                color: "#FF0000",
            });
            await this.loadCalendars();
            this.selectedCalendar = this.calendars.filter(calendar => calendar.id === response.result)[0];
            this.newCalendarName = ''; // Reset the input field
        } catch (error) {
            console.error('Error creating calendar', JSON.stringify(error) + 'calendar name: ' + this.newCalendarName);
        }
    }

i get this error

Failed to save calendar: Error Domain=EKErrorDomain Code=17 "That account does not allow calendars to be added or removed." UserInfo={NSLocalizedDescription=That account does not allow calendars to be added or removed.}
{"message":"[CapacitorCalendar.createCalendar(_:)] Could not create calendar","errorMessage":"[CapacitorCalendar.createCalendar(_:)] Could not create calendar"}

UPDATE

I didn't have any calendars on my iphone, i've just few calendars with google calendars, so i've tried to create manually a new calendar "Test" and in the Calendars Options inside iphone i set "TEST" as the default calendar (create manually on iPhone) now i can create all the calendars i want....

i think there is a sort of default calendar system, i think before with the google as default calendar the plugin was trying to create the calendar on Google, because a calendar I had on Google was set as the default calendar.

How can we prevent this situation?

If by chance a user is in the same situation as me, i.e. with the default calendar on Google are we screwed?

aparajita commented 2 weeks ago

@ebarooni Looks like the EKSource has to be specified. This isn't the exact code, just what ChatGPT suggested.

func createCalendar(eventStore: EKEventStore) {
        guard let iCloudSource = eventStore.sources.first(where: { $0.sourceType == .calDAV && $0.title == "iCloud" }) else {
            print("iCloud source not found")
            return
        }

        let calendar = EKCalendar(for: .event, eventStore: eventStore)
        calendar.title = "My iCloud Calendar"

        // Instead of eventSource.defaultCalendarForNewEvents?.source
        calendar.source = iCloudSource

        do {
            try eventStore.saveCalendar(calendar, commit: true)
            print("Calendar created successfully")
        } catch {
            print("Error creating calendar: \(error.localizedDescription)")
        }
}

Note that if the user is not signed into iCloud, the .sourceType may be .local.

ebarooni commented 2 weeks ago

Thanks @iamandreadompe and @aparajita for the clarifications. I will include this for the next release.