Skip to content

Instantly share code, notes, and snippets.

@johnclayton
Last active December 19, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnclayton/5936468 to your computer and use it in GitHub Desktop.
Save johnclayton/5936468 to your computer and use it in GitHub Desktop.
A convenient wrapper around the sandbox for OSX and iOS apps
@implementation QSandbox
+ (NSString *) documentsDirectory {
return [self directoryInUserSearchPath:NSDocumentDirectory];
}
+ (NSString *) applicationSupportDirectory {
return [self directoryInUserSearchPath:NSApplicationSupportDirectory];
}
+ (NSString *)cachesDirectory {
return [self directoryInUserSearchPath:NSCachesDirectory];
}
+ (NSString *) tempDirectory {
return NSTemporaryDirectory();
}
+ (BOOL) createDocumentsDirectory {
NSError *error = nil;
BOOL created = [self createDirectoryInUserSearchPath:NSDocumentDirectory error:&error];
NSAssert2(created, @"Could not create documents directory! %@ (%@)",[error localizedDescription],[error userInfo]);
return created;
}
+ (BOOL) createApplicationSupportDirectory {
NSError *error = nil;
BOOL created = [self createDirectoryInUserSearchPath:NSApplicationSupportDirectory error:&error];
NSAssert2(created, @"Could not create application support directory! %@ (%@)",[error localizedDescription],[error userInfo]);
return created;
}
+ (BOOL) createCachesDirectory {
NSError *error = nil;
BOOL created = [self createDirectoryInUserSearchPath:NSCachesDirectory error:&error];
NSAssert2(created, @"Could not create caches directory! %@ (%@)",[error localizedDescription],[error userInfo]);
return created;
}
+ (NSString *) directoryInUserSearchPath:(NSUInteger)directoryIdentifier {
NSArray *paths = NSSearchPathForDirectoriesInDomains(directoryIdentifier, NSUserDomainMask, YES);
NSString *directory = [paths lastObject];
return directory;
}
+ (BOOL) createDirectoryInUserSearchPath:(NSUInteger)directoryIdentifier error:(NSError **)error {
NSString *directory = [self directoryInUserSearchPath:directoryIdentifier];
NSFileManager *fm = [NSFileManager new];
BOOL created = YES;
if(NO == [fm fileExistsAtPath:directory isDirectory:NULL]) {
__autoreleasing NSError *localError = nil;
created = [fm createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:NULL error:&localError];
FSQAssert(created, @"Could not create caches directory! %@ (%@)",[localError localizedDescription],[localError userInfo]);
if (error) {
*error = localError;
}
}
return created;
}
+ (unsigned long long) freeSpaceForDirectoryInUserSearchPath:(NSUInteger)directoryIdentifier {
unsigned long long space = 0;
NSString *directory = [self directoryInUserSearchPath:directoryIdentifier];
NSFileManager *fm = [NSFileManager new];
NSError *error = nil;
NSDictionary *attributes = [fm attributesOfFileSystemForPath:directory error:&error];
NSAssert2(attributes, @"Could not get file system attributes! %@ (%@)",[error localizedDescription],[error userInfo]);
if (attributes) {
space = [[attributes objectForKey:NSFileSystemFreeSize] unsignedLongLongValue];
}
return space;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment