1

The following is what I use to get the available storage space on an iOS device for my app:

NSDictionary *dict = [[NSFileManager defaultManager] fileSystemAttributesAtPath:@"/var"];
NSNumber *freeSpace = [dict valueForKey:@"NSFileSystemFreeSize"];

However, the value freeSpace does not correspond with the one shown in the Settings app. The value is always greater than the value shown by Settings. For example, freeSpace is approximately 600,000,000 bytes, where Settings shows 357 MB.

Why is this and how can I get the same value as the value shown by Settings?

Brad Larson
  • 168,330
  • 45
  • 388
  • 563
user1562079
  • 35
  • 1
  • 5
  • Does this help: http://stackoverflow.com/questions/5712527/ios-how-to-detect-total-available-free-disk-space-on-the-iphone-ipad-device – Kev Aug 01 '12 at 01:28
  • I think that iOS may be reserving approx. 200MB for sustaining myself. Thanks for your kindness. – user1562079 Aug 23 '12 at 02:23
  • so do you end up offsetting by 200Mb? I'm a little bit unlucky as my offset varies between 200Mb to 800Mb when I test across different devices... This really get me stucked... – Zennichimaro Jan 25 '13 at 02:59
  • I'm seeing the same thing in one of my applications. – kcharwood Jul 30 '13 at 21:57

4 Answers4

4

I have the same result on iPhone5 with iOS 6.1.4.

double freeSpaceMB = -1.;     
long long freeSpace =
            [[[[NSFileManager defaultManager]
            attributesOfFileSystemForPath:NSHomeDirectory()
                                    error:nil] objectForKey:NSFileSystemFreeSize] longLongValue];
        freeSpaceMB = (freeSpace * 1.)/ (1024 * 1024);   

X - Value from Settings (Usage)

I always get freeSpaceMB = X + 200.

I guess it is something reserve space in iOS. But it is just guessing.

0x8BADF00D
  • 5,725
  • 2
  • 36
  • 34
  • 2
    we are also seeing about a 200 MB over reporting vs itunes/ settings > about > available space, the attributesOfFileSystemForPath dictionary. – Jon Jan 08 '15 at 15:05
4

Under iOS 11, there are new volume capacity keys that can be passed to URL.resourceValues(forKeys:) that provide values that match what is available in device settings.

  • static let volumeAvailableCapacityKey: URLResourceKey Key for the volume’s available capacity in bytes (read-only).

  • static let volumeAvailableCapacityForImportantUsageKey: URLResourceKey Key for the volume’s available capacity in bytes for storing important resources (read-only).

  • static let volumeAvailableCapacityForOpportunisticUsageKey: URLResourceKey Key for the volume’s available capacity in bytes for storing nonessential resources (read-only).

  • static let volumeTotalCapacityKey: URLResourceKey Key for the volume’s total capacity in bytes (read-only).

From Apple's documentation:

Overview

Before you try to store a large amount of data locally, first verify that you have sufficient storage capacity. To get the storage capacity of a volume, you construct a URL (using an instance of URL) that references an object on the volume to be queried, and then query that volume.

Decide Which Query Type to Use

The query type to use depends on what's being stored. If you’re storing data based on a user request or resources the app requires to function properly (for example, a video the user is about to watch or resources that are needed for the next level in a game), query against volumeAvailableCapacityForImportantUsageKey. However, if you’re downloading data in a more predictive manner (for example, downloading a newly available episode of a TV series that the user has been watching recently), query against volumeAvailableCapacityForOpportunisticUsageKey.

Construct a Query

Use this example as a guide to construct your own query:

let fileURL = URL(fileURLWithPath:"/")
do {
    let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
    if let capacity = values.volumeAvailableCapacityForImportantUsage {
        print("Available capacity for important usage: \(capacity)")
    } else {
        print("Capacity is unavailable")
    }
} catch {
    print("Error retrieving capacity: \(error.localizedDescription)")
}
Community
  • 1
  • 1
pwc
  • 6,793
  • 3
  • 25
  • 31
2

Another question got posted yesterday that was closed as a duplicate of this one, and that one demonstrates another way you might have gotten this wrong.

What that questioner did was this:

uint64_t freeSpace = (uint64_t)[fileSystemAttributes objectForKey:NSFileSystemFreeSize];

NSLog(@"Free space in bytes: %lld.",freeSpace);

They treated the object itself (more precisely, the object's address in memory) as the number of free bytes. This is wrong; the object is an NSNumber object, which wraps such a number. The correct code would ask the object for its unsignedLongLongValue, which is of the correct type and therefore large enough to hold the correct value.

Community
  • 1
  • 1
Peter Hosey
  • 93,914
  • 14
  • 203
  • 366
  • I still don't get it, on my iOS 5 device, it is still off by 200 Mb to 800 Mb, I am storing it as uint64_t by calling [NSNumber unsignedLongLongValue], but even though I dump the whole NSDictionary from GDB and divide the number manually by (1024 * 1024), it is still the same... :( Please enlighten me... I've tried using fstats as well, but the reported bytes is just the same... Please help... – Zennichimaro Jan 25 '13 at 02:57
  • @Zennichimaro: What size are you expecting, and what number of bytes are you getting? – Peter Hosey Jan 25 '13 at 03:05
  • I'd like to get the same size that is shown on Settings.app, but it is still off. Settings.app shows I've got 6.0 Gb, but my result says 6875455488 Bytes (6.403267 Gb), I've observed result varying by 200 Mb to 800 Mb :( – Zennichimaro Jan 25 '13 at 05:44
  • @Zennichimaro: What do you mean by “varying”? Between runs of the program? During a single run? Between different programs (and if so, which ones)? On the same device, or different devices? – Peter Hosey Jan 25 '13 at 07:17
  • it varies across different devices with different remaining free space sizes. The variation is larger for devices with larger remaining free space. Multiple executions on the same device will yield same result, and a single run on single device will return same result as long as no files are added or deleted. However, I cannot find the relation between the value from Settings.app and the reading, therefore, I have no reliable way to offset the reading to get the same number as the Settings.app – Zennichimaro Jan 25 '13 at 07:48
  • Zennichimaro, I have The same trouble here. Free space doesn't correspond to the settings.app. Difference floats from 150 to 700 MB (always larger). Have you founded any working solutions yet? – Vlad Polyanskiy Sep 20 '14 at 06:29
-2

I bet that you are using 32-bit integer or float types to store your file system size values. This will fail with large enough file sizes. Instead, use unsigned long long or long double types to store these values.

See the corrected version of this answer for how to read these file sizes accurately. The comments on that answer and others there indicate that this returns a value which matches the free size reported by iTunes and other tools.

Community
  • 1
  • 1
Brad Larson
  • 168,330
  • 45
  • 388
  • 563