I'm following the Apple's documented example to figure out how to query the available disk space on my device.
I'm using the code below in my applicationDidFinishLaunchingWithOptions
:
let fileURL = URL(fileURLWithPath:"/")
do {
let values = try fileURL.resourceValues(forKeys: [
.volumeAvailableCapacityKey,
.volumeAvailableCapacityForImportantUsageKey,
.volumeAvailableCapacityForOpportunisticUsageKey,
.volumeTotalCapacityKey
])
print("Available Capacity: \(Float(values.volumeAvailableCapacity!)/1000000000)GB")
print("ImportantUsage Capacity: \(Float(values.volumeAvailableCapacityForImportantUsage!)/1000000000)GB")
print("Opportunistic Capacity: \(Float(values.volumeAvailableCapacityForOpportunisticUsage!)/1000000000)GB")
print("Total Capacity: \(Float(values.volumeTotalCapacity!)/1000000000)GB")
} catch {
print("Error retrieving capacity: \(error.localizedDescription)")
}
This logs the following:
Available Capacity: 3.665879GB
ImportantUsage Capacity: 0.0GB
Opportunistic Capacity: 0.0GB
Total Capacity: 63.989494GB
Why are volumeAvailableCapacityForImportantUsage
and volumeAvailableCapacityForOpportunisticUsage
zero and under what circumstances does this happen?
Background:
- I'm running this experiment on my own 64GB iPhone SE via xCode 10.2.1 (so the total capacity looks correct)
- My iPhone is running iOS11
- iTunes claims my device has 10.27GB 'free'
- I'm trying to figure this out so I know whether my user will have enough space to download a large (40MB+) in-app purchase
Note: This is not the same as this question. I know how to query available space. I want to understand the results of that query.