First up, a general point. You wrote:
The canonical way to query the disk capacity appears to be through
NSFileManager
.
No, that’s an old mechanism which I recommend that you avoid (as a rough guide, if the API uses file paths rather than file URLs, it’s old). The modern way to do this is the
volumeTotalCapacityKey
URL property.
Having said that, it won’t necessary help you here. You wrote:
For one of my apps the disk capacity of the device it is running on is important to know, for example iPhone 6 (16GB) or iPhone 7 (128GB).
I need to stress that this is a marketing value rather than a technical value. I’m not saying that this marketing value is not based on a technical value [1], but rather that its marketing nature affects people’s understanding of it. For example, a technical level getting a value of 15.99999 GB would be fine, but that’s not acceptable at a marketing level.
There is no good way to get this marketing value. So, regardless of what else you do, I recommend that you file an enhancement request for an API that returns this value explicitly (much like the
model
property of
UIDevice
returns the user-visible device model).
Please post your bug number, just for the record.
Beyond that, the problem you’re facing here is twofold. Firstly, iOS has no low-level APIs for interrogating the hardware. On macOS you could use I/O Kit to look at how the hardware fits together, but that API is not available on iOS.
Secondly, iOS devices have multiple volumes and the relationship between those volumes is not documented. On current systems there’s an immutable volume for the system and a writeable volume for apps, documents, and so on. However, these details are not documented and subject to change.
Indeed, this has changed in the past. Before the advent of APFS these volumes were represented by multiple partitions. Thus, to find the total size of the device you had to add up the sizes of each of these partitions. With the advent of APFS each of these volumes is carved out of a shared container. The traditional APIs for getting disk capacity can’t represent this subtlety, so each volume report’s the entire container’s size as its capacity. That’s why you’re getting back (roughly) the total disk size from
NSFileSystemSize
.
As to what you should do here, I don’t have a concrete answer. No matter what you do, it’s easy to imagine the system changing so that things fail in the future, and hence my earlier recommendation to file an ER. My only suggestion is that, after doing that, you open a DTS tech support incident requesting a workaround.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
[1] See the fine print on our various Tech Spec pages.