xcode - 是否有用于 iCloud 磁盘容量和使用情况的 API?

标签 xcode icloud nsfilemanager ios9 cloudkit

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

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

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

最佳答案

容量

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

文件大小

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

根据苹果 docs和 Xcode 中的快速帮助,您似乎想查看 URLForUbiquityContainerIdentifier

Returns the URL for the iCloud container associated with the specified identifier and establishes access to that container. A URL pointing to the specified ubiquity container, or nil if the container could not be located or if iCloud storage is unavailable for the current user or device.



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

1

鉴于这些信息,这样的事情可能会起作用……请务必检查文档、快速帮助和头文件以了解任何其他警告。
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)")
        }
    }
})

关于xcode - 是否有用于 iCloud 磁盘容量和使用情况的 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32428503/

相关文章:

iphone - 使用核心数据和更改值时的 EXC_BAD_ACCESS

iphone - 如何在 iOS 中连接 2 或 3 个音频文件?

ios - iPad 未出现在 Storyboard 中的 "view as"部分

ios - 阻止 iCloud 备份的应用程序

swift - 设置 Alamofire 自定义目标文件名而不是使用 suggestedDownloadDestination

iphone - 申请用户账号注册登录,最好的处理方式?

ios - 在应用程序中备份和还原选件

core-data - iCloud-CoreData 解决冲突

swift - 自定义目录图标 Swift

ios - 创建新文件并使用 NSFileManager objective c 读取它