Skip to content

Commit

Permalink
fix: let NSPredicate handle predicate arguments
Browse files Browse the repository at this point in the history
The old code was crashing when using special characters
such as quotes and newlines. This fix allows the actual
NSPredicate class to handle the arguments.

References https://outsystemsrd.atlassian.net/browse/RNMT-3533
  • Loading branch information
usernuno committed Nov 7, 2019
1 parent 55b9253 commit 93494d8
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/ios/Calendar.m
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,19 @@ - (NSArray*) findEKEventsWithTitle: (NSString *)title
calendars: (NSArray*)calendars {

NSMutableArray *predicateStrings = [NSMutableArray arrayWithCapacity:3];
NSMutableArray *predicateArguments = [NSMutableArray arrayWithCapacity:3];

if (title != (id)[NSNull null] && title.length > 0) {
title = [title stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
[predicateStrings addObject:[NSString stringWithFormat:@"title contains[c] '%@'", title]];
[predicateStrings addObject:@"title contains[c] %@"];
[predicateArguments addObject:title];
}
if (location != (id)[NSNull null] && location.length > 0) {
location = [location stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
[predicateStrings addObject:[NSString stringWithFormat:@"location contains[c] '%@'", location]];
[predicateStrings addObject:@"location contains[c] %@"];
[predicateArguments addObject:location];
}
if (notes != (id)[NSNull null] && notes.length > 0) {
notes = [notes stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
[predicateStrings addObject:[NSString stringWithFormat:@"notes contains[c] '%@'", notes]];
[predicateStrings addObject:@"notes contains[c] %@"];
[predicateArguments addObject:notes];
}

NSString *predicateString = [predicateStrings componentsJoinedByString:@" AND "];
Expand All @@ -322,7 +324,7 @@ - (NSArray*) findEKEventsWithTitle: (NSString *)title
NSArray *datedEvents, *matchingEvents;

if (predicateString.length > 0) {
matches = [NSPredicate predicateWithFormat:predicateString];
matches = [NSPredicate predicateWithFormat:predicateString argumentArray:predicateArguments];

datedEvents = [self.eventStore eventsMatchingPredicate:[eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendars]];

Expand Down

0 comments on commit 93494d8

Please sign in to comment.