EddyVerbruggen / Calendar-PhoneGap-Plugin

:date: Cordova plugin to Create, Change, Delete and Find Events in the native Calendar
774 stars 403 forks source link

Add support for iOS 17 and fix the non-access problem for calendars #566

Open kallewangstedt opened 8 months ago

kallewangstedt commented 8 months ago

Added support for iOS 17 and a fix for the issue described here: https://github.com/EddyVerbruggen/Calendar-PhoneGap-Plugin/issues/565

For the fix to work you need to make sure you ask for permissions in deviceready and the app must be built with iOS17 as a target in XCode.

Do this for deviceready:

document.addEventListener('deviceready', () => {
    try {
        await calendarAccess();
    } catch (error) {
        console.log({error});
    }
});

A suggested function to run async is this:

async function calendarAccess() {
return new Promise((resolve, reject) => {
    // Check if we have permission to access the calendar
    window.plugins.calendar.hasReadWritePermission(
        (result) => {
            if (result === true) {
                // We already have permission
                console.log("We have calendar permissions.");
                resolve(true); // Resolve the promise with true, indicating permission is already granted
            } else {
                // We do not have permission, so let's request it
                window.plugins.calendar.requestReadWritePermission(
                    () => {
                        // Permission request was successful
                        console.log("Calendar permissions granted.");
                        resolve(true); // Resolve the promise with true, indicating permission is granted now
                    },
                    (err) => {
                        // Permission request failed
                        console.error("Calendar permissions were not granted:", err);
                        reject(err); // Reject the promise with the error
                    }
                );
            }
        },
        (err) => {
            // Error occurred while checking permissions
            console.error("Error checking calendar permissions:", err);
            reject(err); // Reject the promise with the error
        }
    );
});
kallewangstedt commented 8 months ago

I guess the rest of the plugin works fine on older OS versions, so no harm in keeping support. I'll update the PR accordingly.

hooliapps commented 8 months ago

Do you think you need to add this to the plugin.xml ? Without this permission (for my case) it don't works.

<preference name="CALENDARS_FULL_ACCESS_USAGE_DESCRIPTION" default=" " />
<config-file target="*-Info.plist" parent="NSCalendarsFullAccessUsageDescription">
  <string>$CALENDARS_FULL_ACCESS_USAGE_DESCRIPTION</string>
</config-file>
matfantinel commented 8 months ago

The issue I'm having with this code is that when I call requestReadWritePermission, it doesn't wait for the permission prompt to complete, and instead errors out, but with a null error. Using the exact same JS code suggested in the first post of this PR, you can see in the screenshot below that the "permissions were not granted" log appears before the "Full access to the event store granted" that is logged in the native code.

Xcode 2023-09-29 at 17 33 48@2x

hooliapps commented 8 months ago

Hello

I have exactly the same behaviour, the callback are called without waiting for the response.

I also see something like Event authorisation given, but i dont know how to "get" it.

Le ven. 29 sept. 2023 à 22:36, Matt Fantinel @.***> a écrit :

The issue I'm having with this code is that when I call requestReadWritePermission, it doesn't wait for the permission prompt to complete, and instead errors out, but with a null error. Using the exact same JS code suggested in the first post of this PR, you can see in the screenshot below that the "permissions were not granted" log appears before the "Full access to the event store granted" that is logged in the native code.

[image: Xcode 2023-09-29 at 17 33 @.*** https://user-images.githubusercontent.com/24247035/271715311-ab044846-a653-40cf-9fd6-a84069054fb6.png

— Reply to this email directly, view it on GitHub https://github.com/EddyVerbruggen/Calendar-PhoneGap-Plugin/pull/566#issuecomment-1741460735, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJN5KOS2AVXSHTHR223LQUDX44WNPANCNFSM6AAAAAA5IJS6YU . You are receiving this because you commented.Message ID: @.***>

sjregan commented 8 months ago

I have created a new pull request to address the callbacks issue.

matfantinel commented 8 months ago

@sjregan does that Pull Request completely replace this one?

sjregan commented 8 months ago

@matfantinel yes

hooliapps commented 8 months ago

Hello

Can you check the code please ?

I tested and seems that "callbacks" are not ok, but they are ok with this version: https://github.com/EddyVerbruggen/Calendar-PhoneGap-Plugin/pull/567