Query Available iOS Disk Space with Swift

Query Available iOS Disk Space with Swift

iOS 11 Update

The answers given below no longer provide accurate results 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: NSHomeDirectory() as String)
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)")
}


Original Answer

Optional binding with if let works here as well.

I would suggest that the function returns an optional Int64, so that it can return
nil to signal a failure:

func deviceRemainingFreeSpaceInBytes() -> Int64? {
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
if let systemAttributes = NSFileManager.defaultManager().attributesOfFileSystemForPath(documentDirectoryPath.last as String, error: nil) {
if let freeSize = systemAttributes[NSFileSystemFreeSize] as? NSNumber {
return freeSize.longLongValue
}
}
// something failed
return nil
}

Swift 2.1 Update:

func deviceRemainingFreeSpaceInBytes() -> Int64? {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
guard
let systemAttributes = try? NSFileManager.defaultManager().attributesOfFileSystemForPath(documentDirectory),
let freeSize = systemAttributes[NSFileSystemFreeSize] as? NSNumber
else {
// something failed
return nil
}
return freeSize.longLongValue
}

Swift 3.0 Update:

func deviceRemainingFreeSpaceInBytes() -> Int64? {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!
guard
let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: documentDirectory),
let freeSize = systemAttributes[.systemFreeSize] as? NSNumber
else {
// something failed
return nil
}
return freeSize.int64Value
}

Usage:

if let bytes = deviceRemainingFreeSpaceInBytes() {
print("free space: \(bytes)")
} else {
print("failed")
}

How to get the Total Disk Space and Free Disk space using AttributesOfFileSystemForpaths in swift 2.0

For Swift 5.1.3:

struct FileManagerUility {

static func getFileSize(for key: FileAttributeKey) -> Int64? {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)

guard
let lastPath = paths.last,
let attributeDictionary = try? FileManager.default.attributesOfFileSystem(forPath: lastPath) else { return nil }

if let size = attributeDictionary[key] as? NSNumber {
return size.int64Value
} else {
return nil
}
}

static func convert(_ bytes: Int64, to units: ByteCountFormatter.Units = .useGB) -> String? {
let formatter = ByteCountFormatter()
formatter.allowedUnits = units
formatter.countStyle = ByteCountFormatter.CountStyle.decimal
formatter.includesUnit = false
return formatter.string(fromByteCount: bytes)
}

}

Use the api like this:

 if let totalSpaceInBytes = FileManagerUility.getFileSize(for: .systemSize) {
/// If you want to convert into GB then call like this
let totalSpaceInGB = FileManagerUility.convert(totalSpaceInBytes)
print("Total space [\(totalSpaceInBytes) bytes] = [\(totalSpaceInGB!) GB]")

}

if let freeSpaceInBytes = FileManagerUility.getFileSize(for: .systemFreeSize) {
/// If you want to convert into GB then call like this
let freeSpaceInGB = FileManagerUility.convert(freeSpaceInBytes)
print("Free space [\(freeSpaceInBytes) bytes] = [\(freeSpaceInGB!) GB]")
}

How to detect total available/free disk space on the iPhone/iPad device?

UPDATE: Since a lot of time has passed after this answer and new methods/APIs have been added, please check the updated answers below for Swift etc; Since I've not used them myself, I can't vouch for them.

Original answer:
I found the following solution working for me:

-(uint64_t)getFreeDiskspace {
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];

if (dictionary) {
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
} else {
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}

return totalFreeSpace;
}

It returns me exactly the size that iTunes displays when device is connected to machine.

Is there any possibility to read iOS device Storage information

Use below UIDevice extension:

extension UIDevice {
func MBFormatter(_ bytes: Int64) -> String {
let formatter = ByteCountFormatter()
formatter.allowedUnits = ByteCountFormatter.Units.useMB
formatter.countStyle = ByteCountFormatter.CountStyle.decimal
formatter.includesUnit = false
return formatter.string(fromByteCount: bytes) as String
}

//MARK: Get String Value
var totalDiskSpaceInGB:String {
return ByteCountFormatter.string(fromByteCount: totalDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.decimal)
}

var freeDiskSpaceInGB:String {
return ByteCountFormatter.string(fromByteCount: freeDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.decimal)
}

var usedDiskSpaceInGB:String {
return ByteCountFormatter.string(fromByteCount: usedDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.decimal)
}

var totalDiskSpaceInMB:String {
return MBFormatter(totalDiskSpaceInBytes)
}

var freeDiskSpaceInMB:String {
return MBFormatter(freeDiskSpaceInBytes)
}

var usedDiskSpaceInMB:String {
return MBFormatter(usedDiskSpaceInBytes)
}

//MARK: Get raw value
var totalDiskSpaceInBytes:Int64 {
guard let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String),
let space = (systemAttributes[FileAttributeKey.systemSize] as? NSNumber)?.int64Value else { return 0 }
return space
}

/*
Total available capacity in bytes for "Important" resources, including space expected to be cleared by purging non-essential and cached resources. "Important" means something that the user or application clearly expects to be present on the local system, but is ultimately replaceable. This would include items that the user has explicitly requested via the UI, and resources that an application requires in order to provide functionality.
Examples: A video that the user has explicitly requested to watch but has not yet finished watching or an audio file that the user has requested to download.
This value should not be used in determining if there is room for an irreplaceable resource. In the case of irreplaceable resources, always attempt to save the resource regardless of available capacity and handle failure as gracefully as possible.
*/
var freeDiskSpaceInBytes:Int64 {
if #available(iOS 11.0, *) {
if let space = try? URL(fileURLWithPath: NSHomeDirectory() as String).resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey]).volumeAvailableCapacityForImportantUsage {
return space ?? 0
} else {
return 0
}
} else {
if let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String),
let freeSpace = (systemAttributes[FileAttributeKey.systemFreeSize] as? NSNumber)?.int64Value {
return freeSpace
} else {
return 0
}
}
}

var usedDiskSpaceInBytes:Int64 {
return totalDiskSpaceInBytes - freeDiskSpaceInBytes
}

}

How to get Total/Available/Used space of a USB drive in MacOS - Swift?

You can use FileManager's mountedVolumeURLs method to get all mounted volumes and get the volumeAvailableCapacityForImportantUsage resource key/value from it:



extension FileManager {
static var mountedVolumes: [URL] {
(FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil) ?? []).filter({$0.path.hasPrefix("/Volumes/")})
}
}


extension URL {
var volumeTotalCapacity: Int? {
(try? resourceValues(forKeys: [.volumeTotalCapacityKey]))?.volumeTotalCapacity
}
var volumeAvailableCapacityForImportantUsage: Int64? {
(try? resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey]))?.volumeAvailableCapacityForImportantUsage
}
var name: String? {
(try? resourceValues(forKeys: [.nameKey]))?.name
}

}

Usage:

for url in FileManager.mountedVolumes {
print(url.name ?? "Untitled")
print("Capacity:", url.volumeTotalCapacity ?? "nil")
print("Available:", url.volumeAvailableCapacityForImportantUsage ?? "nil")
print("Used:", (try? url.sizeOnDisk()) ?? "nil") // check the other link below
}

For a 16GB USB drive with BigSur installer the code above will print

Install macOS Big Sur

Capacity: 15180193792

Available: 2232998976

Used: 12.93 GB on disk


To get the used space of a volume "sizeOnDisk" you can check this post

Is there a way to determine the amount of disk space an app has used in iOS?

I ended up figuring out how to do this:

I created a category on NSFileManager and added:

-(NSUInteger)applicationSize
NSString *appgroup = @"Your App Group"; // Might not be necessary in your case.

NSURL *appGroupURL = [self containerURLForSecurityApplicationGroupIdentifier:appgroup];
NSURL *documentsURL = [[self URLsForDirectory: NSDocumentDirectory inDomains: NSUserDomainMask] firstObject];
NSURL *cachesURL = [[self URLsForDirectory: NSCachesDirectory inDomains: NSUserDomainMask] firstObject];

NSUInteger appGroupSize = [appGroupURL fileSize];
NSUInteger documentsSize = [documentsURL fileSize];
NSUInteger cachesSize = [cachesURL fileSize];
NSUInteger bundleSize = [[[NSBundle mainBundle] bundleURL] fileSize];
return appGroupSize + documentsSize + cachesSize + bundleSize;
}

I also added a category on NSURL with the following:

-(NSUInteger)fileSize
{
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:self.path isDirectory:&isDir];
if (isDir)
return [self directorySize];
else
return [[[[NSFileManager defaultManager] attributesOfItemAtPath:self.path error:nil] objectForKey:NSFileSize] unsignedIntegerValue];
}

-(NSUInteger)directorySize
{
NSUInteger result = 0;
NSArray *properties = @[NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:self includingPropertiesForKeys:properties options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
for (NSURL *url in files)
{
result += [url fileSize];
}

return result;
}

It takes a bit to run if you have a lot of app data, but it works.

Check if there is enough free space on users phone to record a video

I needed similar functionality, so I created function that returns number of free MB. I tested what happens when space is filled and found out that iOS starts showing notifications about no space left after about 120MB, and when that happen Camera stops working. But my app could save other files until it was reached about 30MB of free space.
So to be sure, it is recommended that when you get value from this function, check it if it is at least 200(MB) and then allow user to save.
I tested both functions(for Swift 5.0 and Objective C) in Xcode 11.0 on iPhone X and 6s and they worked very well.

In Swift 5.0, also works with lower Swift versions(with slight changes depending on version)

func getFreeSpace() -> CLongLong // return free space in MB
{
var totalFreeSpaceInBytes: CLongLong = 0; //total free space in bytes

do{
let spaceFree: CLongLong = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())[FileAttributeKey.systemFreeSize] as! CLongLong;
totalFreeSpaceInBytes = spaceFree;

}catch let error{ // Catch error that may be thrown by FileManager
print("Error is ", error);
}

let totalBytes: CLongLong = 1 * CLongLong(totalFreeSpaceInBytes);
let totalMb: CLongLong = totalBytes / (1024 * 1024);

return totalMb;
}

In Objective C:

-(uint64_t)getFreeDiskspace { // return free space in MB
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];

if (dictionary) {
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
uint64_t totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
uint64_t totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
NSLog(@"Memory Capacity: %llu MiB , Free: %llu MiB", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
return ((totalFreeSpace/1024ll)/1024ll);
} else {
NSLog(@"Error getting memory info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}

return 0;
}

How to detect total available/free disk space on the iPhone/iPad device on iOS 11

So if people have the problem of not getting the correct free size, use NSURL resourceValuesForKeys to get the free space.

[ fileURL resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey ] error:&error];

double = availableSizeInBytes = [ results[NSURLVolumeAvailableCapacityForImportantUsageKey] doubleValue ];

Reference Why is `volumeAvailableCapacityForImportantUsage` zero?



Related Topics



Leave a reply



Submit