Hi,
I develop a feature to get the iPhone's total storage. After some researching, the way I can get the total storage of iPhone is using this code.
class DiskStatus {
/// Helper method to query against a resource value key
private static func getVolumeResourceValues(for key: URLResourceKey) -> URLResourceValues? {
let fileUrl = URL(fileURLWithPath: "/")
let results = try? fileUrl.resourceValues(forKeys: [key])
return results
}
/// Volume’s total capacity in bytes.
public static var totalCapacity: Int? {
get {
let resourceValues = getVolumeResourceValues(for: .volumeTotalCapacityKey)
return resourceValues?.volumeTotalCapacity
}
}
}
When I print the totalCapacity, its value is 254807724032 bytes. If I convert it to GB using decimal system it will be 254.8GB.
When I looked into Settings, the total storage of my iPhone is 256GB.
My questions are:
- Why the total storage shown in Settings different with my code result?
- How to achieve so that I can show exact value in Settings?
Thank you.
Why the total storage shown in Settings different with my code result?
Because of various bits of overhead. It’s very rare for a volume to take up an entire disk. Most disks are partitioned and that introduces some overhead. And there can other forms of overhead beyond that. Bootable volumes typically have a partition that facilitates booting, there might be a partition for swap, and so on.
The exact details of this are not documented and change regularly.
How to achieve so that I can show exact value in Settings?
There’s no good way to do that. There are obviously heuristics you can apply — take the value and round it, for example — but there’s no API that returns the exact value shown in Settings. If you’d like to see an API added in the future, I encourage you to file an enhancement request describing your requirements.
Please post your bug number, just for the record.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"