From 9cadf9530d6f54a424e19940eaf8c8f8154ae3da Mon Sep 17 00:00:00 2001 From: gotomanners Date: Thu, 21 Sep 2017 16:30:52 +0100 Subject: [PATCH] Added method to validate user search input, preventing app crash isSearchTermValid can run a series of validations on the search term before searching. Currently only validating against searching single or multiple blank spaces. This prevents against an app crash. --- Reader/TextSearch/SearchManager.m | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Reader/TextSearch/SearchManager.m b/Reader/TextSearch/SearchManager.m index b65f206b..db015157 100755 --- a/Reader/TextSearch/SearchManager.m +++ b/Reader/TextSearch/SearchManager.m @@ -224,6 +224,19 @@ -(void)searchForTerm:(NSString*)term page:(NSUInteger)page { }); } +-(BOOL)isSearchTermValid:(NSString*)term { + BOOL result = YES; + + // Can run mutiple validations on the search term below + + // Validate against searching single or multiple blank spaces + NSCharacterSet * notWhiteSpaceSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet]; + NSRange rangeOfNonWhitespaceChars = [self.searchTerm rangeOfCharacterFromSet:notWhiteSpaceSet]; + result = rangeOfNonWhitespaceChars.location != NSNotFound; + + return result; +} + -(void)startSearch { if(self.running) { @@ -239,8 +252,14 @@ -(void)startSearch { NSNotification * notification = [NSNotification notificationWithName:kNotificationSearchDidStart object:self userInfo:info]; [[NSNotificationCenter defaultCenter] postNotification:notification]; - // Start the search - [self searchForTerm:self.searchTerm page:self.currentPage]; + // Validate search term before searching + if([self isSearchTermValid: self.searchTerm]) { + // Start the search + [self searchForTerm:self.searchTerm page:self.currentPage]; + } else { + [self stopSearch]; + return; + } } #pragma mark - Lifecycle