Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
This chapter is from the book

One More Thing: Checking for Available Disk Space

The NSFileManager class enables you to determine how much space is free on the iPhone and how much space is provided on the device as a whole. Listing 1-1 demonstrates how to check for these values and show the results using a friendly comma-formatted string. The values returned represent the free space in bytes.

Listing 1-1. Recovering File System Size and File System Free Size

- (NSString *) commaFormattedStringWithLongLong: (long long) num
{
    // Produce a properly formatted number string
    // Alternatively use NSNumberFormatter
    if (num < 1000)
        return [NSString stringWithFormat:@"%d", num];
    return    [[self commasForNumber:num/1000]
        stringByAppendingFormat:@",%03d", (num % 1000)];
}

- (void) action: (UIBarButtonItem *) bbi
{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSDictionary *fattributes =
        [fm fileSystemAttributesAtPath:NSHomeDirectory()];
    NSLog(@"System space: %@",
        [self commaFormattedStringWithLongLong:[[fattributes
        objectForKey:NSFileSystemSize] longLongValue]]);
    NSLog(@"System free space: %@",
        [self commasForNumber:[[fattributes
        objectForKey:NSFileSystemFreeSize] longLongValue]]);
}
  • + Share This
  • 🔖 Save To Your Account