Query Available iOS Disk Space with Swift

后端 未结 4 2057
野趣味
野趣味 2020-12-02 18:56

I\'m trying to get the available iOS device storage using Swift. I found this function here

        func deviceRemainingFreeSpaceInBytes() ->         


        
4条回答
  •  醉话见心
    2020-12-02 19:45

    Well, according to the above codes:

    let usedSpace = totalDiskSpaceInBytes - freeDiskSpaceInBytes
    

    you might find out that usedSpace doesn't equal the value of iPhone setting page. That is because in iOS11, Apple introduces Total available capacity in bytes for "Important" resources.

    Total available capacity in bytes for "Important" resources, including space expected to be cleared by purging non-essential and cached resources. "Important" means something that the user or application clearly expects to be present on the local system, but is ultimately replaceable. This would include items that the user has explicitly requested via the UI, and resources that an application requires in order to provide functionality.

    Examples: A video that the user has explicitly requested to watch but has not yet finished watching or an audio file that the user has requested to download.

    This value should not be used in determining if there is room for an irreplaceable resource. In the case of irreplaceable resources, always attempt to save the resource regardless of available capacity and handle failure as gracefully as possible.

    In order to get the exact same value as what we see in iPhone setting page, we can get free space by volumeAvailableCapacityForImportantUsage

    if let space = try? URL(fileURLWithPath: NSHomeDirectory() as String).resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey]).volumeAvailableCapacityForImportantUsage {
        return space ?? 0
    }
    

    You can use the following UIDevice extension:

    Swift4

    extension UIDevice {
        func MBFormatter(_ bytes: Int64) -> String {
            let formatter = ByteCountFormatter()
            formatter.allowedUnits = ByteCountFormatter.Units.useMB
            formatter.countStyle = ByteCountFormatter.CountStyle.decimal
            formatter.includesUnit = false
            return formatter.string(fromByteCount: bytes) as String
        }
        
        //MARK: Get String Value
        var totalDiskSpaceInGB:String {
           return ByteCountFormatter.string(fromByteCount: totalDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.decimal)
        }
        
        var freeDiskSpaceInGB:String {
            return ByteCountFormatter.string(fromByteCount: freeDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.decimal)
        }
        
        var usedDiskSpaceInGB:String {
            return ByteCountFormatter.string(fromByteCount: usedDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.decimal)
        }
        
        var totalDiskSpaceInMB:String {
            return MBFormatter(totalDiskSpaceInBytes)
        }
        
        var freeDiskSpaceInMB:String {
            return MBFormatter(freeDiskSpaceInBytes)
        }
        
        var usedDiskSpaceInMB:String {
            return MBFormatter(usedDiskSpaceInBytes)
        }
        
        //MARK: Get raw value
        var totalDiskSpaceInBytes:Int64 {
            guard let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String),
                let space = (systemAttributes[FileAttributeKey.systemSize] as? NSNumber)?.int64Value else { return 0 }
            return space
        }
        
        /*
         Total available capacity in bytes for "Important" resources, including space expected to be cleared by purging non-essential and cached resources. "Important" means something that the user or application clearly expects to be present on the local system, but is ultimately replaceable. This would include items that the user has explicitly requested via the UI, and resources that an application requires in order to provide functionality.
         Examples: A video that the user has explicitly requested to watch but has not yet finished watching or an audio file that the user has requested to download.
         This value should not be used in determining if there is room for an irreplaceable resource. In the case of irreplaceable resources, always attempt to save the resource regardless of available capacity and handle failure as gracefully as possible.
         */
        var freeDiskSpaceInBytes:Int64 {
            if #available(iOS 11.0, *) {
                if let space = try? URL(fileURLWithPath: NSHomeDirectory() as String).resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey]).volumeAvailableCapacityForImportantUsage {
                    return space ?? 0
                } else {
                    return 0
                }
            } else {
                if let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String),
                let freeSpace = (systemAttributes[FileAttributeKey.systemFreeSize] as? NSNumber)?.int64Value {
                    return freeSpace
                } else {
                    return 0
                }
            }
        }
        
        var usedDiskSpaceInBytes:Int64 {
           return totalDiskSpaceInBytes - freeDiskSpaceInBytes
        }
    
    }
    

    usage:

    print("totalDiskSpaceInBytes: \(UIDevice.current.totalDiskSpaceInBytes)")
    print("freeDiskSpace: \(UIDevice.current.freeDiskSpaceInBytes)")
    print("usedDiskSpace: \(UIDevice.current.usedDiskSpaceInBytes)")
    
    
        
    

提交回复
热议问题