How To Find Available Disk Space of iOS Device?

Aasim Naseem 2

Hello iOS fellows;
Hope life is as much bright as the retina display of iPhone 4S;  (:

Well its a Valentine’s night and I’m writing this blog post at mid night, thinking that may be someone get some benefit with these few lines and saved his/her time to googling here and there; Ahh what a priorities of an engineer; (:

I was working on an iOS application where I had needed to determine the available disk space on user’s device, to warn the user about storage;

Here is the code I used that finds outs the space in bytes;

[sourcecode lang=”objc” highlight=”6,7″]

+(void)calculateSpaceOnDisk {
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
if (dictionary) {
float freeSpace = [[dictionary objectForKey: NSFileSystemFreeSize] floatValue];
float totalSpace = [[dictionary objectForKey: NSFileSystemSize] floatValue];
} else {
NSLog(@"Error Obtaining File System Info: Domain = %@, Code = %@", [error domain], [error code]);
}//end calculateSpaceOnDisk:


The highlighted lines are soul of this post;

Note that I’m storing all of my application’s data in Library/Cache directory, not in /Document; Not sure right now how it may effect on above piece of code;

Stay bright;

free counters

Comments 2

Leave a Reply

Your email address will not be published.