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

后端 未结 18 2011
别跟我提以往
别跟我提以往 2020-11-22 11:14

I\'m looking for a better way to detect available/free disk space on the iPhone/iPad device programmatically.
Currently I\'m using the NSFileManager to detect the disk s

18条回答
  •  暖寄归人
    2020-11-22 11:52

    Following code is Swift 3.0 version implementation of the answer previously provided by ChrisJF:

    func freeSpaceInBytes() -> NSString {
    
        var remainingSpace = NSLocalizedString("Unknown", comment: "The remaining free disk space on this device is unknown.")
    
        do {
            let dictionary =  try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())
            let freeSpaceSize = ((dictionary[FileAttributeKey.systemFreeSize] as AnyObject).longLongValue)!
            remainingSpace = ByteCountFormatter.string(fromByteCount: freeSpaceSize, countStyle: ByteCountFormatter.CountStyle.file)
        }
        catch let error {
            NSLog(error.localizedDescription)
        }
    
        return remainingSpace as NSString
    
    }
    

提交回复
热议问题