Check CloudKit Private Storage Capacity

I'm uploading assets to a private cloud database, but I'd like to check the account's storage availability before having the user upload.


Any way to programmatically check the remaining capacity of a user's iCloud account?

Replies

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

})