How To Find Available Disk Space of iOS Device?

Posted on Updated on

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;


+(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

Advertisements

2 thoughts on “How To Find Available Disk Space of iOS Device?

    Ali Raza said:
    February 14, 2012 at 09:44

    Good work, very nice;

    Ans responded:
    February 14, 2012 at 18:11

    JazakAllah Ali … 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s