Skip to content

Instantly share code, notes, and snippets.

@leoniralves
Created October 17, 2022 13:06
Show Gist options
  • Save leoniralves/d8c5a8c365c2919f9d98c8f10a93a103 to your computer and use it in GitHub Desktop.
Save leoniralves/d8c5a8c365c2919f9d98c8f10a93a103 to your computer and use it in GitHub Desktop.
iOS Disk Status (Storage)
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