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

fix: uses new method for requesting calendar permissions on iOS 17 #448

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ npm-debug.log
.DS_Store
android/build/*
xcuserdata
.idea
50 changes: 31 additions & 19 deletions ios/RNCalendarEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ - (NSString *)hexStringFromColor:(UIColor *)color {
g = 1;
b = 1;
}

return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
lroundf(r * 255),
lroundf(g * 255),
Expand Down Expand Up @@ -225,12 +225,12 @@ - (NSDictionary *)buildAndSaveEvent:(NSDictionary *)details options:(NSDictionar
NSDictionary *geo = [locationOptions valueForKey:@"coords"];
CLLocation *geoLocation = [[CLLocation alloc] initWithLatitude:[[geo valueForKey:@"latitude"] doubleValue]
longitude:[[geo valueForKey:@"longitude"] doubleValue]];

calendarEvent.structuredLocation = [EKStructuredLocation locationWithTitle:[locationOptions valueForKey:@"title"]];
calendarEvent.structuredLocation.geoLocation = geoLocation;
calendarEvent.structuredLocation.radius = [[locationOptions valueForKey:@"radius"] doubleValue];
}

return [self saveEvent:calendarEvent options:options];
}

Expand Down Expand Up @@ -620,7 +620,7 @@ - (NSDictionary *)serializeCalendarEvent:(EKEvent *)event
@catch (NSException *exception) {
NSLog(@"RNCalendarEvents encountered an issue while serializing event (attendees) '%@': %@", event.title, exception.reason);
}

@try {
if (event.hasAlarms) {
NSMutableArray *alarms = [[NSMutableArray alloc] init];
Expand Down Expand Up @@ -697,7 +697,7 @@ - (NSDictionary *)serializeCalendarEvent:(EKEvent *)event
[formedCalendarEvent setValue:[NSNumber numberWithBool:event.isDetached] forKey:_isDetached];

[formedCalendarEvent setValue:[NSNumber numberWithBool:event.allDay] forKey:_allDay];

@try {
if (event.hasRecurrenceRules) {
EKRecurrenceRule *rule = [event.recurrenceRules objectAtIndex:0];
Expand All @@ -724,9 +724,9 @@ - (NSDictionary *)serializeCalendarEvent:(EKEvent *)event
@catch (NSException *exception) {
NSLog(@"RNCalendarEvents encountered an issue while serializing event (recurrenceRules) '%@': %@", event.title, exception.reason);
}

[formedCalendarEvent setValue:[self availabilityStringMatchingConstant:event.availability] forKey:_availability];

@try {
if (event.structuredLocation && event.structuredLocation.radius) {
NSMutableDictionary *structuredLocation = [[NSMutableDictionary alloc] initWithCapacity:3];
Expand Down Expand Up @@ -780,14 +780,26 @@ - (NSDictionary *)serializeCalendarEvent:(EKEvent *)event

RCT_EXPORT_METHOD(requestPermissions:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
NSString *status = granted ? @"authorized" : @"denied";
if (!error) {
resolve(status);
} else {
reject(@"error", @"authorization request error", error);
}
}];

if (@available(iOS 17.0, *)) {
[self.eventStore requestFullAccessToEventsWithCompletion:^(BOOL granted, NSError *error) {
NSString *status = granted ? @"authorized" : @"denied";
if (!error) {
resolve(status);
} else {
reject(@"error", @"authorization request error", error);
}
}];
} else {
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
NSString *status = granted ? @"authorized" : @"denied";
if (!error) {
resolve(status);
} else {
reject(@"error", @"authorization request error", error);
}
}];
}
}

RCT_EXPORT_METHOD(findCalendars:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
Expand Down Expand Up @@ -1008,12 +1020,12 @@ - (NSDictionary *)serializeCalendarEvent:(EKEvent *)event
reject(@"error", @"unauthorized to access calendar", nil);
return;
}

__weak RNCalendarEvents *weakSelf = self;
dispatch_async(serialQueue, ^{
@try {
RNCalendarEvents *strongSelf = weakSelf;

NSMutableDictionary *details = [NSMutableDictionary dictionaryWithDictionary:settings];
[details setValue:title forKey:_title];

Expand All @@ -1037,12 +1049,12 @@ - (NSDictionary *)serializeCalendarEvent:(EKEvent *)event
reject(@"error", @"unauthorized to access calendar", nil);
return;
}

__weak RNCalendarEvents *weakSelf = self;
dispatch_async(serialQueue, ^{
@try {
RNCalendarEvents *strongSelf = weakSelf;

Boolean futureEvents = [RCTConvert BOOL:options[@"futureEvents"]];
NSDate *exceptionDate = [RCTConvert NSDate:options[@"exceptionDate"]];

Expand Down