If you have an application that allows users to upload content to the app, you might want to show the user how much space is available for them to use.
This is especially true on the iPhone where it is not as easy for the user to quickly check if they have enough room to upload a given file.
You may also want to check if there is enough space before you start the upload, so you can inform them that the file wont fit.
That brings you to the task of figuring out how much free space there is, and more importantly how much free space is left that you as an unprivileged program can access.
The following code is based towards the iPhone, however all that would be needed to change it to be used on the Mac is the location you are checking for free space, and the setting of the label.
NOTE: The below “shown” code is old. The updated code is in the zip file at the bottom
#include <sys/param.h> #include <sys/mount.h>
NSString *sizeType; float availableDisk; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); struct statfs tStats; statfs([[paths lastObject] cString], &tStats); availableDisk = (float)(tStats.f_bavail * tStats.f_bsize); if (availableDisk > 1024) { //Kilobytes availableDisk = availableDisk / 1024; sizeType = @" KB"; } if (availableDisk > 1024) { //Megabytes availableDisk = availableDisk / 1024; sizeType = @" MB"; } if (availableDisk > 1024) { //Gigabytes availableDisk = availableDisk / 1024; sizeType = @" GB"; } diskSpaceLbl.text = [[@"Available Disk Space: " stringByAppendingFormat:@"%.2f", availableDisk] stringByAppendingString:sizeType];
This will give you something like the following:
![]()
UPDATE:
I have gone a step further and made this into an easy to use class, that works out of the box with the iPhone and the Mac.






