使用Swift查询可用的iOS磁盘空间

使用Swift查询可用的iOS磁盘空间,第1张

使用Swift查询可用的iOS磁盘空间 iOS 11更新

在iOS
11下,下面给出的答案不再提供准确的结果。可以传递新的卷容量键,以

URL.resourcevalues(forKeys:)
提供与设备设置中可用值匹配的值。

  • static let volumeAvailableCapacityKey: URLResourceKey
    卷的可用容量的键(以字节为单位)(只读)。

  • static let volumeAvailableCapacityForimportantUsageKey: URLResourceKey
    卷的可用容量(以字节为单位)的键,用于存储重要资源(只读)。

  • static let volumeAvailableCapacityForOpportunisticUsageKey: URLResourceKey
    卷的可用容量(以字节为单位)的键,用于存储非必需资源(只读)。

  • static let volumeTotalCapacityKey: URLResourceKey
    卷总容量的键(以字节为单位)(只读)。

从Apple的文档中:

总览

在尝试在本地存储大量数据之前,请先验证您是否具有足够的存储容量。为了获得卷的存储容量,您可以构造一个URL(使用URL实例),该URL引用要查询的卷上的对象,然后查询该卷。

确定要使用的查询类型

使用的查询类型取决于要存储的内容。如果您要根据用户请求或应用程序正常运行所需的资源来存储数据(例如,用户即将观看的视频或游戏中下一关所需的资源),请查询

volumeAvailableCapacityForimportantUsageKey
。但是,如果您以更具预测性的方式下载数据(例如,下载用户最近一直在观看的电视连续剧的最新剧集),请查询
volumeAvailableCapacityForOpportunisticUsageKey

构造查询

使用此示例作为指导来构造您自己的查询:

let fileURL = URL(fileURLWithPath: NSHomeDirectory() as String)do {    let values = try fileURL.resourcevalues(forKeys: [.volumeAvailableCapacityForimportantUsageKey])    if let capacity = values.volumeAvailableCapacityForimportantUsage {        print("Available capacity for important usage: (capacity)")    } else {        print("Capacity is unavailable")    }} catch {    print("Error retrieving capacity: (error.localizedDescription)")}

原始答案

可选绑定

if let
这里也适用。

我建议该函数返回一个optional

Int64
,以便它可以返回
nil
以表示失败:

func deviceRemainingFreeSpaceInBytes() -> Int64? {    let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .UserDomainMask, true)    if let systemAttributes = NSFileManager.defaultManager().attributesOfFileSystemForPath(documentDirectoryPath.last as String, error: nil) {        if let freeSize = systemAttributes[NSFileSystemFreeSize] as? NSNumber { return freeSize.longLongValue        }    }    // something failed    return nil}

Swift 2.1更新:

func deviceRemainingFreeSpaceInBytes() -> Int64? {    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .UserDomainMask, true).last!    guard        let systemAttributes = try? NSFileManager.defaultManager().attributesOfFileSystemForPath(documentDirectory),        let freeSize = systemAttributes[NSFileSystemFreeSize] as? NSNumber    else {        // something failed        return nil    }    return freeSize.longLongValue}

Swift 3.0更新:

func deviceRemainingFreeSpaceInBytes() -> Int64? {    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!    guard        let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: documentDirectory),        let freeSize = systemAttributes[.systemFreeSize] as? NSNumber    else {        // something failed        return nil    }    return freeSize.int64Value}

用法:

if let bytes = deviceRemainingFreeSpaceInBytes() {    print("free space: (bytes)")} else {    print("failed")}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5622485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存