From db84f8945fc936677084307333a14e9460439310 Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 6 Feb 2024 15:41:02 +0100 Subject: [PATCH] fix: uses new method for requesting calendar permissions on iOS 17 --- .gitignore | 1 + ios/RNCalendarEvents.m | 50 ++++++++++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 769f3f9..e91a9ff 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ npm-debug.log .DS_Store android/build/* xcuserdata +.idea diff --git a/ios/RNCalendarEvents.m b/ios/RNCalendarEvents.m index 4b67ddb..b153165 100644 --- a/ios/RNCalendarEvents.m +++ b/ios/RNCalendarEvents.m @@ -60,7 +60,7 @@ - (NSString *)hexStringFromColor:(UIColor *)color { g = 1; b = 1; } - + return [NSString stringWithFormat:@"#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), @@ -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]; } @@ -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]; @@ -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]; @@ -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]; @@ -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) @@ -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]; @@ -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"]];