I’m having trouble figuring out how to use this function to get the available disk space on my iphone. I just don’t know how to populate the “fromBookmarkData:” parameter. I can’t find an example of it in use anywhere. Here is what I have so far …
`if itemsArray[indexPath.row].fileSize > URL.resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey], fromBookmarkData: <#Data#>) {`
`// alert user not enough space for file`
`return`
`}`
Thanks for the response. Yeah, I tried using the function without the bookmark parameter but I get an error. It insists that I use the bookmark parameter. I’m using all the latest in X Code. Targeting IOS 11.4 with Swift 4.2.
I wonder if that is because I am trying to run it on the simulator?
The “fromBookmarkData” version of resourceValues is a class function, called using the class URL.
The version of resourceValues that just needs “forKeys” is an instance function, called using an instance of URL, not the class itself.
If you are planning to save a file on the phone, you will need an instance of URL to do that. So create the instance, and then use that to call resourceValues(forKeys:).
One way or another, by bookmark or URL instance, the call needs to know what storage device you are looking at. It does not assume local storage.
Hi @brduffy,
here’s a sample snippet that you can run in Playgrounds or on the terminal
var url = URL(fileURLWithPath: NSHomeDirectory() as String)
if let res = try? url.resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey]) {
print(res.volumeAvailableCapacityForImportantUsage ?? 0)
}