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

后端 未结 18 2053
别跟我提以往
别跟我提以往 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:44

    ChrisJF answer in Swift 2.1 version:

    func freeSpaceInBytes() -> NSString{
    
        var remainingSpace = NSLocalizedString("Unknown", comment: "The remaining free disk space on this device is unknown.")
    
        do {
    
            let dictionary =  try NSFileManager.defaultManager().attributesOfFileSystemForPath(NSHomeDirectory())
            freeSpaceSize = (dictionary[NSFileSystemFreeSize]?.longLongValue)!
            remainingSpace = NSByteCountFormatter.stringFromByteCount(freeSpaceSize, countStyle: NSByteCountFormatterCountStyle.File)
    
        }
        catch let error as NSError {
    
            error.description
            NSLog(error.description)
    
        }
    
        return remainingSpace
    
    }
    

提交回复
热议问题