20

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.

1 Answer 1

1

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:

1

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)")
        }
    }
})
3
  • Sorry for the long wait. While this does technically work, it returns the size of the local disk, not the cloud container. I'll update this post if I find a suitable solution.
    – W Dyson
    Mar 8, 2016 at 20:12
  • @WDyson ~ I've made a couple of revisions based on your comment. Hope this solves it for you.
    – Tommie C.
    Mar 9, 2016 at 0:55
  • Why have you decided that what you get is iCloud capacity? Aug 10, 2021 at 15:03

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.