Mac/iPhone: Show Available Usable Disk Space

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:

availablediskspace

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.

FSStats.zip

7 responses to “Mac/iPhone: Show Available Usable Disk Space

  1. Dennis

    Hi Dallas. Thanks for sharing this. I tried running the classes you posted in an application, but it always crashes (both simulator and device). Have others got this working as is?

  2. I just tried the code in the zip file in a mac project and it worked correctly for me…
    If I remember right it does not work in the simulator but does work on the device.

    Make sure you are using the zip file and not the pasted code.

    Here is what I added to my project to get it to work.

    (added the files to the project)
    (added: #include “FSStats.h” – to the class where you want to use it)

    NSLog(@”%@”, [[[FSStats alloc] initWithFilePath:@”/Users/dallas”] getAvailableDiskSpace]);

    Make sure you set the path correctly and you have permissions (I dont remember what happens if the path is incorrect or if you dont have permissions, so check this to start)

    Hope this helps

  3. Dennis

    Thanks for taking time to respond.

  4. David Schiefer

    For me, it did not work. Both the old code and the new code made the simulator and iPhone crash.

    2009-12-06 13:25:13.446 [6536:207] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[NSCFString stringByAppendingString:]: nil argument’
    2009-12-06 13:25:13.449 [6536:207] Stack: (
    30524507,
    2531120393,
    30608443,
    30608282,
    236131,
    67276,
    10880,
    2868767,
    2906634,
    2894728,
    2877139,
    2904245,
    37531345,
    30309248,
    30305352,
    2870889,
    2908163,
    9458,
    9330
    )

  5. Raptor

    Hi Dallas, I used your codes, and sometimes it returns negative free space. why?

  6. I installed the class. Must I use initWithFilePath before using getAvailableDiskSpace? If so, what path should I specify?

    Even though you provide an init method, I had to use [[ alloc] init]. Is that what you intended?

    Thanks,
    Scott

  7. Pingback: Got wrong value while detecting available disk space on iphone | PHP Developer Resource

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>