Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Wednesday, December 18, 2013

iOS Get Space Remaining on Disk

So, you want to programmatically determine how much room is left on your iOS device, huh?

If you're writing an app that uses a lot of space on the device, its going to be nice to know how much room you have left, such as when you're recording audio via your app. I wrote a simple recording app that shows approximately how many hours are left of recording given the remaining disk space. That requires some knowledge about sampling rates and such, but all you really want is to get the space remaining on disk. I have just the ticket for that.

Here's an example of getting the file space remaining on your iOS device, which I use to great effect:


  NSError *err = nil;
  NSArray *nsSearchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  
  NSString *targetSearchPath = [nsSearchPaths lastObject];
  
  NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:targetSearchPath error: &err];
  
  if (dictionary) {
    NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
    uint64_t freeSpaceOnDisk = [freeFileSystemSizeInBytes unsignedLongLongValue];
  
    long kb = 1024ll;
    long mb = kb * kb;
  
    NSLog(@"%llu MB Free memory available.", (freeSpaceOnDisk/mb));
  
    // Now, do something interesting with this data
    
  } else {
      NSLog(@"Error %@", err);
  }

Well, that wasn't too painful at all, was it? When I found the answer originally, it took me quite a while to get there. Here's hoping you got there faster than me.

If you're an audiophile who hates cats, then you probably come here often because we like to listen to music too, and this is a cat-free zone. Internet high-five! You should come around here more often, which is why you should give us a +1, and we'll see you next time!




About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates: http://www.blindnsdate.com

Popular Posts

Designed By Seo Blogger Templates