Skip to content

Instantly share code, notes, and snippets.

@tempelmann
Created July 20, 2021 14:15
Show Gist options
  • Save tempelmann/76fb08c106557c3b02b0a17c527356a6 to your computer and use it in GitHub Desktop.
Save tempelmann/76fb08c106557c3b02b0a17c527356a6 to your computer and use it in GitHub Desktop.
Code used in Find Any File to test whether Full Disk Access is enabled
+ (BOOL)hasFullDiskAccess
{
if (@available(macOS 10.14, *)) {
// fall thru
} else {
return YES; // always true pre-10.14
}
//
// The following code was kindly provided by Pierre Bernard, author of AccessKit, Tembo and HoudahSpot
//
static NSArray *directoriesToCheck = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSArray *directoryPaths = @[
@"~/Library/Application Support/com.apple.TCC/",
@"~/Library/Containers/com.apple.mail/",
@"~/Library/Containers/com.apple.Safari/",
@"~/Library/Cookies/",
@"~/Library/Mail/",
@"~/Library/Messages/",
@"~/Library/Metadata/CoreSpotlight/",
@"~/Library/Safari/"
];
NSMutableArray *expandedPaths = [NSMutableArray arrayWithCapacity:[directoryPaths count]];
for (NSString *path in directoryPaths) {
NSString *expandedPath = [[path stringByExpandingTildeInPath] stringByStandardizingPath];
[expandedPaths addObject:expandedPath];
}
directoriesToCheck = [expandedPaths copy];
});
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL directoryExists = NO;
for (NSString *directoryPath in directoriesToCheck) {
BOOL isDirectory;
if (![fileManager fileExistsAtPath:directoryPath isDirectory:&isDirectory]) continue;
if (!isDirectory) continue;
directoryExists = YES;
NSError *error;
NSArray *contents = [fileManager contentsOfDirectoryAtPath:directoryPath error:&error];
if (contents == nil) {
NSError *underlyingError = error.userInfo[NSUnderlyingErrorKey];
if ([underlyingError.domain isEqualToString:NSPOSIXErrorDomain] && (underlyingError.code == 1 /* operation not permitted */)) {
return NO;
}
}
}
return directoryExists;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment