1) Enable iCloud service in the project.
2)
In the Info.plist, add the following settings. Please change the first "key" value (follow the same value as it was created in the Apple developer web portal) and also the folder name "My test folder".
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.my.myTest180614</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>My test folder</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
3) Download the open source library from the following URL. You need to add 4 files in iCloud folder (except the pch file).
https://github.com/iRareMedia/iCloudDocumentSync
4) Next thing is to follow the sample codes in the above open source home page.
Notes: whenever you made changes to the iCloud settings, make sure you increase the app build number. Otherwise, the changes might not reflect in iCloud service.
5) Upon app init, make sure that you run the following codes to init the iCloud service.
[[iCloud sharedCloud] setupiCloudDocumentSyncWithUbiquityContainer:nil];
[iCloud sharedCloud].verboseLogging = YES;
BOOL cloudIsAvailable = [[iCloud sharedCloud] checkCloudAvailability];
if (cloudIsAvailable) {
[self showLog:@"cloud is available"];
}
else {
[self showLog:@"cloud is NOT available"];
}
6) To upload a file to iCloud:
[[iCloud sharedCloud] uploadLocalDocumentToCloudWithName:@"my-test-file-local.txt" completion:^(NSError* err) {
if (err == nil) {
NSLog(@"error..");
}
}];
7) To check the file if it exist in the iCloud:
NSURL* cloud_doc = [[f URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Documents/my-test-file-local.txt"];
BOOL b = [f isUbiquitousItemAtURL:cloud_doc];
if (b) {
[self showLog:@"=> my-test-file-local.txt- the file exist in the cloud"];
}
else {
[self showLog:@"=> my-test-file-local.txt- the file does NOT exist in the cloud"];
}
Notes: by default, the file stores in "Documents" folder!! Watch out for this behaviour.
8) To check if all files have been synced to local, make sure you handle the following delegate:
iCloudDelegate
Handle this callback before start doing anything to the files (from the cloud):
iCloudFileUpdateDidEnd