I am struggling here to make this work, i have to detect the string which i am receiving in the response is base64 encoded or not and handle it accordingly.
I have tried two ways so far:
1.
if ([input length] % 4 == 0 && input.length >= 3) {
static NSCharacterSet *invertedBase64CharacterSet = nil;
if (invertedBase64CharacterSet == nil) {
invertedBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="]invertedSet];
}
return [input rangeOfCharacterFromSet:invertedBase64CharacterSet options:NSLiteralSearch].location == NSNotFound;
}
return NO;
2.
NSString *expression = regexString;
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
if (numberOfMatches > 0) {
return YES;
} else {
return NO;
}
Everything is working fine except when the string is 4 characters in length like 'Adam' both above snippets considers this as a base64 string which is not correct.
If anyone know better way to detect the Base64 string please let me know and it should work for string of 4 characters in length also.
Thanks in advance.