There is an Apple document that is mandatory for all aspects of http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/iCloud/iCloud.html
One major issue with the code presented here is that it does not have a command in the identifier specified for the URLForUbiquityContainerIdentifier, even if it is mentioned in the checklist, leaving it completely populated from the rights, it seems like the best aproach.
Personally, the only changes I had to make to get iCloud in my application:
- check the "use iCloud" checkbox on the developer's website for my app ID.
- load restored position for this application id
- check "enable rights" in xcode summary
That’s all, here’s probably a clearer code example (should work for both iOS and OSX):
NSURL *url = [self getiCloudURLFor:@"foo.bar" containerID:nil]; //leaving nil so it is auto filled from entitlements if (url) { NSError *error; if (![[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:url error:&error]) { NSLog(@"Error downloading/syncing %@ (%@)",[url path],[error description]); }else{ NSLog(@"Started downloading/syncing %@",[url path]); } } NSArray *conflicts = [NSFileVersion unresolvedConflictVersionsOfItemAtURL:url]; for (NSFileVersion *conflict in conflicts) { NSLog(@"Conflicting %@ at %@ by %@ from %@",[url path],[conflict URL],[conflict localizedNameOfSavingComputer],[conflict modificationDate]); } - (NSURL*)getiCloudURLFor:(NSString*)fileName containerID:(NSString*)containerID { NSFileManager *fm = [NSFileManager defaultManager]; NSURL *rootURL = [fm URLForUbiquityContainerIdentifier:containerID]; if (rootURL) { NSURL *directoryURL = [rootURL URLByAppendingPathComponent:@"Documents"]; if (![fm fileExistsAtPath:[directoryURL path]]) [fm createDirectoryAtURL:directoryURL withIntermediateDirectories:NO attributes:nil error:NULL]; NSURL *cloudURL = [directoryURL URLByAppendingPathComponent:fileName]; if (![fm isUbiquitousItemAtURL:cloudURL]) [self makeUbiquitousItemAtURL:cloudURL];//this only runs once per filename when it is first added to iCloud return cloudURL; }else{ return [[[fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0] URLByAppendingPathComponent:fileName]; //no cloud } return nil; } - (void)makeUbiquitousItemAtURL:(NSURL*)cloudURL { NSFileManager *fm = [NSFileManager defaultManager]; NSURL *localURL = [[[fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0] URLByAppendingPathComponent:[cloudURL lastPathComponent]]; if (![fm fileExistsAtPath:[localURL path]]) [fm createFileAtPath:[localURL path] contents:nil attributes:nil]; NSError *error; if(![fm setUbiquitous:YES itemAtURL:localURL destinationURL:cloudURL error:&error]) { NSLog(@"Error making %@ ubiquituous at %@ (%@)",[localURL path],[cloudURL path],[error description]); }else{ NSLog(@"Made %@ ubiquituous at %@",[localURL lastPathComponent],[cloudURL path]); } }
source share