Created
October 30, 2014 17:02
-
-
Save psobko/1609e6568768fa01b25d to your computer and use it in GitHub Desktop.
Get device storage using NSFileManager API. This code will print the results of each possible NSSearchPathDirectory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
void logStorageForLocation(NSSearchPathDirectory location) { | |
NSError *error; | |
NSArray * const paths = NSSearchPathForDirectoriesInDomains(location, NSUserDomainMask, YES); | |
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] | |
error:&error]; | |
uint64_t totalSpace = 0; | |
uint64_t totalFreeSpace = 0; | |
NSNumber *fileSystemSizeInBytes = [attributes objectForKey: NSFileSystemSize]; | |
NSNumber *freeFileSystemSizeInBytes = [attributes objectForKey:NSFileSystemFreeSize]; | |
totalSpace = [fileSystemSizeInBytes unsignedLongLongValue]; | |
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue]; | |
NSLog(@"Location: %d - Total space %llu MB / Free Space %llu MB", location, ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll)); | |
} | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
logStorageForLocation(NSApplicationDirectory); | |
logStorageForLocation(NSDemoApplicationDirectory); | |
logStorageForLocation(NSDeveloperApplicationDirectory); | |
logStorageForLocation(NSAdminApplicationDirectory); | |
logStorageForLocation(NSLibraryDirectory); | |
logStorageForLocation(NSDeveloperDirectory); | |
logStorageForLocation(NSUserDirectory); | |
logStorageForLocation(NSDocumentationDirectory); | |
logStorageForLocation(NSDocumentDirectory); | |
logStorageForLocation(NSCoreServiceDirectory); | |
logStorageForLocation(NSAutosavedInformationDirectory); | |
logStorageForLocation(NSDesktopDirectory); | |
logStorageForLocation(NSCachesDirectory); | |
logStorageForLocation(NSApplicationSupportDirectory); | |
logStorageForLocation(NSDownloadsDirectory); | |
logStorageForLocation(NSInputMethodsDirectory); | |
logStorageForLocation(NSMoviesDirectory); | |
logStorageForLocation(NSMusicDirectory); | |
logStorageForLocation(NSPicturesDirectory); | |
logStorageForLocation(NSPrinterDescriptionDirectory); | |
logStorageForLocation(NSSharedPublicDirectory); | |
logStorageForLocation(NSPreferencePanesDirectory); | |
// logStorageForLocation(NSApplicationScriptsDirectory); | |
logStorageForLocation(NSItemReplacementDirectory); | |
logStorageForLocation(NSAllApplicationsDirectory); | |
logStorageForLocation(NSAllLibrariesDirectory); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment