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;
}