What is the path to your iCloud Documents folder

iCloud-IconTo attain the path to your iCloud Documents folder (or ubiquitous documents folder) you need to know your Team ID.

You’ll also need the Bundle ID which can be extracted from the main bundle.

Here’s a method that will deliver you the path to a file. All you need to do is provide the name of a file and it will return the full path to this file in iCloud:

- (NSString *)grabCloudPath:(NSString *)filename {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *teamID = @"F34xxxxxxx"; // replace with your real Team ID
    NSString *bundleID = [[NSBundle mainBundle]bundleIdentifier];
    NSString *cloudRoot = [NSString stringWithFormat:@"%@.%@", teamID, bundleID];
    
    NSURL *cloudRootURL = [fileManager URLForUbiquityContainerIdentifier:cloudRoot];
    
    NSString *pathToCloudFile = [[cloudRootURL path]stringByAppendingPathComponent:@"Documents"];
    pathToCloudFile = [pathToCloudFile stringByAppendingPathComponent:filename];
    
    return pathToCloudFile;
}

Useful for Core Data Store Files, UIDocuments or plain images.

Note than when documents have populated to iOS devices they need to be downloaded explicitly – otherwise you won’t get a useable file (unlike on OS X which is known as a “greedy peer” and will download the entire file, not just the wrapper).

For completion, here’s how you do just that:

// make sure file is downloaded completely
NSString *cloudPath = [self grabCloudPath:filename];
NSURL *cloudURL = [NSURL fileURLWithPath:cloudPath];
[[NSFileManager defaultManager]startDownloadingUbiquitousItemAtURL:cloudURL error:nil];




Leave a Reply