A quick Internet search to find out how to get the disk space on a device showed a number of blog posts that proposed solutions similar to this:
#include <sys/param.h> #include <sys/mount.h> +(float)getTotalDiskSpaceInBytes { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); struct statfs tStats; statfs([[paths lastObject] cString], &tStats); float totalSpace = (float)(tStats.f_blocks * tStats.f_bsize); return totalSpace; }
This approach relies on the handy Unix statfs() function that provides the required file system information. I coded it up and it worked on my 3.x devices. Fine I thought, I’m off and running. Then when testing on a 2.x device, it crashed. The problem looks to be differences in the compiler settings between the two OS versions. Rather than figure that out (I leave that as an exercise to the reader 🙂 ), I continued my search and came across what I consider the “correct” solution since it uses the iPhone SDK itself, as given below.
+(float)getTotalDiskSpaceInBytes { float totalSpace = 0.0f; NSError *error = nil; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error]; if (dictionary) { NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize]; totalSpace = [fileSystemSizeInBytes floatValue]; } else { DLog(@"Error Obtaining File System Info: Domain = %@, Code = %@", [error domain], [error code]); } return totalSpace; }
As stated in the documentation, these interfaces wrapper the statfs() function, so they ultimately do the same thing, but they have been a part of the SDK since version 2.0! Testing verified my assumption and this approach works for me on my 2.x and 3.x devices and the numbers match what iTunes shows as well.
August 4th, 2010 at 21:26
How can I print both of them to int
August 8th, 2010 at 19:13
@Will: [NSString stringWithFormat:@”%.0f”, diskSpace];
June 28th, 2011 at 04:37
Nice one!!
It really works to find total disk space in iphone.
But how to find used disk space?
Please help. I am new to programming.
June 28th, 2011 at 18:58
@Aditya: If you have the total disk space and the available disk space, then calculating the used disk space is just a simple matter of subtraction.
August 11th, 2011 at 11:01
I get crazy scientific notation looking numbers returned when attempting to use this on 4.3 SDK.. What is the latest you have tried this on? Any suggestions?
Thanks,
Critter
August 12th, 2011 at 07:54
@Critter: I have not tested this code in a while. Probably, as the post states, iOS 3.x.
November 14th, 2011 at 01:53
How to get the memory occupied by the current app in iphone
April 6th, 2012 at 01:00
I got an error like this
Error Obtaining File System Info: Domain = NSCocoaErrorDomain, Code = 260, UserInfo = {
NSFilePath = “/var/root/Documents”;
NSUnderlyingError = “Error Domain=NSPOSIXErrorDomain Code=2 \”The operation couldn\U2019t be completed. No such file or directory\””;
}
I found that no directory /var/root/Documents in the device
Thanks
August 27th, 2012 at 20:13
To get the FREE amount of space, use “NSFileSystemFreeSize” instead of “NSFileSystemSize” as the key in the dictionary.
@Ben: Sounds like you were using the system instead of user domain mask in the call to “NSSearchPathForDirectoriesInDomains”. You can also use the NSCachesDirectory for testing (w/in app sandbox = User domain mask).