【问题标题】:Is there an API for iCloud disk capacity and usage?是否有用于 iCloud 磁盘容量和使用的 API?
【发布时间】:2015-12-02 09:49:33
【问题描述】:

我使用 NSFileManager 来获取当前设备磁盘空间(总、已使用和空闲),就像这样......

let systemAttributes = try? NSFileManager.defaultManager().attributesOfFileSystemForPath(NSHomeDirectory() as String)
    let space = (systemAttributes?[NSFileSystemSize] as? NSNumber)?.longLongValue

有没有办法为用户的 iCloud 帐户做类似的事情?我希望能够在应用中显示用户的 iCloud 磁盘空间统计信息。

【问题讨论】:

    标签: xcode icloud nsfilemanager ios9 cloudkit


    【解决方案1】:

    容量

    iCloud 在线容量由 Apple 决定。本地缓存大小由磁盘上的限制设置。我之前的回答提到了磁盘容量。您的样本应该返回您自己设备的大小。

    文件大小

    以下答案已修改,以帮助您确定 iCloud 容器路径的文件大小。还有其他方法,例如枚举可能是您想要的子路径。我在下面展示了应该工作的最简单的东西works(我还没有测试过)。如果您正在寻找不同的东西,请告诉我。

    根据 Apple docs 和 Xcode 中的快速帮助,您可能需要查看 URLForUbiquityContainerIdentifier

    返回与指定关联的 iCloud 容器的 URL 标识符并建立对该容器的访问。指向的 URL 指定的泛在容器,如果容器不能是 nil 如果当前用户无法使用 iCloud 存储,或者 设备。

    关于使用此属性的重要说明:

    鉴于这些信息,这样的事情可能会起作用...请务必查看文档、快速帮助和头文件以了解任何其他警告。

    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), { () -> Void in
    
        //Note: The container identifier is in your Capabilities > iCloud > Containers
        if let iCloudPath = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier("iCloud.com.yourcompanydomain.app")?.path {
    
        let pathAttributes = try? NSFileManager.defaultManager().attributesOfItemAtPath(iCloudPath)
    
        let space = (pathAttributes?[NSFileSize] as? NSNumber)?.unsignedLongLongValue
    
            dispatch_async(dispatch_get_main_queue()) {
                print ("iCloud disk space: \(space)")
            }
        }
    })
    

    【讨论】:

    • 抱歉让您久等了。虽然这在技术上确实有效,但它返回的是本地磁盘的大小,而不是云容器的大小。如果我找到合适的解决方案,我会更新这篇文章。
    • @WDyson ~ 我根据您的评论做了一些修改。希望这可以为您解决。
    • 你为什么决定你得到的是 iCloud 容量?
    猜你喜欢
    • 1970-01-01
    • 2016-09-03
    • 2012-07-10
    • 2014-03-23
    • 1970-01-01
    • 2020-05-03
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多