是否有用于iCloud磁盘容量和使用的API? [英] Is there an API for iCloud disk capacity and usage?

查看:114
本文介绍了是否有用于iCloud磁盘容量和使用的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

是否可以对用户的iCloud帐户执行类似的操作?我希望能够在应用程序内显示用户的iCloud磁盘空间统计信息。

解决方案

容量



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



FileSize



以下答案已修改为帮助您确定iCloud容器路径的文件大小。还有其他一些方法,例如枚举子路径,这可能是您想要的。我在下面介绍了最应该起作用的最简单的方法(我尚未测试)。让我知道您是否在寻找不同的东西。



根据Apple



鉴于该信息,这样的方法可能会起作用...请务必检查文档,快速帮助和头文件是否有其他警告。 / p>

  dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),{()-> 

中的无效//注意:如果让iCloudPath = NSFileManager.defaultManager()。URLForUbiquityContainerIdentifier( iCloud.com.yourcompanydomain.app)?. path {
$,则容器标识符在您的功能> iCloud>容器
中。 b $ b let pathAttributes =试试吗? leManager.defaultManager()。attributesOfItemAtPath(iCloudPath)

let space =(pathAttributes?[NSFileSize] as? NSNumber)?. unsignedLongLongValue

dispatch_async(dispatch_get_main_queue()){
print( iCloud磁盘空间:\(space))
}
}
})


I use NSFileManager to get the current device disk space (total, used, and free) like so...

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

Is there any way to do something similar for a user's iCloud account? I want to be able to present a user's iCloud disk space statistics within an app.

解决方案

Capacity

iCloud online capacity is determined by Apple. The local cache size is set by the limits on your disk. My earlier answer referenced disk capacity. Your sample should be returning the size of your own device.

FileSize

The answer below has been revised to help you determine the file size of the iCloud container path. There are other methods like enumerating the subpaths which may be what you want. I am presenting the simplest thing that should work works below (I haven't tested it). Let me know if you are looking for something different.

According to Apple docs and the Quick help in Xcode it seems you might want to look at the 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.

An important note about using this property:

Given that information, something like this may work...Be sure to check the documentation, quick help and header files for any additional caveats.

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)")
        }
    }
})

这篇关于是否有用于iCloud磁盘容量和使用的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆