Created
October 17, 2022 13:06
-
-
Save leoniralves/d8c5a8c365c2919f9d98c8f10a93a103 to your computer and use it in GitHub Desktop.
iOS Disk Status (Storage)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DiskStatus { | |
// MARK: Get String Value | |
class var totalDiskSpace: String { | |
get { | |
return ByteCountFormatter.string(fromByteCount: totalDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.file) | |
} | |
} | |
class var freeDiskSpace: String { | |
get { | |
return ByteCountFormatter.string(fromByteCount: freeDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.file) | |
} | |
} | |
class var usedDiskSpace: String { | |
get { | |
return ByteCountFormatter.string(fromByteCount: usedDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.file) | |
} | |
} | |
// MARK: Get raw value | |
class var totalDiskSpaceInBytes: Int64 { | |
get { | |
do { | |
let systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String) | |
let space = (systemAttributes[FileAttributeKey.systemSize] as? NSNumber)?.int64Value | |
return space ?? 0 | |
} catch { | |
return 0 | |
} | |
} | |
} | |
class var freeDiskSpaceInBytes: Int64 { | |
get { | |
do { | |
let systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String) | |
let freeSpace = (systemAttributes[FileAttributeKey.systemFreeSize] as? NSNumber)?.int64Value | |
return freeSpace ?? 0 | |
} catch { | |
return 0 | |
} | |
} | |
} | |
class var usedDiskSpaceInBytes: Int64 { | |
get { | |
let usedSpace = totalDiskSpaceInBytes - freeDiskSpaceInBytes | |
return usedSpace | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment