我使用 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.
关于使用此属性的重要说明:
鉴于这些信息,这样的事情可能会起作用……请务必检查文档、快速帮助和头文件以了解任何其他警告。
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/