Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kallewangstedt
Copy link

@kallewangstedt kallewangstedt commented Sep 26, 2023

Added support for iOS 17 and a fix for the issue described here: #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 kallewangstedt changed the title Update Calendar.m Add support for iOS 17 and fix the non-access problem for calendars Sep 26, 2023
hooliapps added a commit to hooliapps/Calendar-PhoneGap-Plugin that referenced this pull request Sep 27, 2023
Copy link

@mkocik mkocik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about backward compatibility with older ios (<17)? Shouldn't we keep it?

    EKEventStore* eventStoreCandidate = [[EKEventStore alloc] init];
    if (@available(iOS 17.0, *)) {
        [eventStoreCandidate requestFullAccessToEventsWithCompletion:^(BOOL granted, NSError *error) {
            if (granted) {
                self.eventStore = eventStoreCandidate;
                NSLog(@"Full access to the event store granted and eventStore initialized.");
            } else {
                NSLog(@"Access to the event store not granted. Error: %@", error.localizedDescription);
            }
        }];
    } else {
        __block BOOL accessGranted = NO;
          if([eventStoreCandidate respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
            dispatch_semaphore_t sema = dispatch_semaphore_create(0);
            [eventStoreCandidate requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
              accessGranted = granted;
              dispatch_semaphore_signal(sema);
            }];
            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
          } else { // we're on iOS 5 or older
            accessGranted = YES;
          }
        
          if (accessGranted) {
            self.eventStore = eventStoreCandidate;
          }
    }

`

@kallewangstedt
Copy link
Author

kallewangstedt commented Sep 29, 2023

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
Copy link

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
Copy link

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
Copy link

hooliapps commented Sep 29, 2023 via email

@sjregan
Copy link

sjregan commented Oct 3, 2023

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

@matfantinel
Copy link

@sjregan does that Pull Request completely replace this one?

@sjregan
Copy link

sjregan commented Oct 3, 2023

@matfantinel yes

@hooliapps
Copy link

Hello

Can you check the code please ?

I tested and seems that "callbacks" are not ok, but they are ok with this version: #567

@Ardavan92 Ardavan92 mentioned this pull request Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants