␡
- Accessing Basic Device Information
- Adding Device Capability Restrictions
- Recipe: Checking Device Proximity and Battery States
- Recipe: Recovering Additional Device Information
- Recipe: Using Acceleration to Locate "Up"
- Working with Basic Orientation
- Retrieving the Current Accelerometer Angle Synchronously
- Recipe: Using Acceleration to Move Onscreen Objects
- Recipe: Accelerometer-Based Scroll View
- Recipe: Core Motion Basics
- Recipe: Retrieving and Using Device Attitude
- Detecting Shakes Using Motion Events
- Recipe: Using External Screens
- Tracking Users
- One More Thing: Checking for Available Disk Space
- Summary
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]]); }