I'm writing a Unity plugin which makes use of iCloud.
Now the way I currently use to move files to iCloud is using this code:
[byteDocument saveToURL:savePathURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success)
{
if (ubiquitousContainerURL != nil)
{
NSURL *ubiquityDocumentDirectory = [ubiquitousContainerURL
URLByAppendingPathComponent:@"Documents"
isDirectory:YES];
ubiquityDocumentDirectory = [ubiquityDocumentDirectory URLByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", filename, extension]];
NSFileManager* fm = [NSFileManager defaultManager];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError* error = nil;
if (![fm fileExistsAtPath:ubiquityDocumentDirectory.path])
{
NSLog(@"Attemping to move to cloud");
didMoveToCloud = [fm setUbiquitous:YES itemAtURL:savePathURL destinationURL:ubiquityDocumentDirectory error:nil];
}
else
{
NSLog(@"Attemping to replace in cloud");
didMoveToCloud = [fm replaceItemAtURL:ubiquityDocumentDirectory withItemAtURL:savePathURL backupItemName:nil options:0 resultingItemURL:nil error:&error];
}
if (didMoveToCloud)
{
NSLog(@"Did move text document successfully to cloud");
//UnitySendMessage("GameObject", "DidSaveFile", [savePathURL.path cStringUsingEncoding:NSASCIIStringEncoding]);
}
else
{
NSLog(@"Failed to move text document to cloud");
//UnitySendMessage("GameObject", "DidSaveFile", [savePathURL.path cStringUsingEncoding:NSASCIIStringEncoding]);
}
if (error != nil)
{
NSLog(@"Error saving text document to cloud: %@", error.description);
//UnitySendMessage("GameObject", "DidSaveFile", [savePathURL.path cStringUsingEncoding:NSASCIIStringEncoding]);
}
});
}
}];
Basically I'm just using a simple inherited class of UIDocument called CloudByteDocument. I first save it locally on the device and then move it to iCloud afterwards.
Now the problem is that using SetUbiquituos does not seem to provide any way of tracking the upload progress? I'm already attached to both MSMetaDataQuery observerser, that is:
// Listen for the second phase live-updating
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(queryDidReceiveNotification:) name:NSMetadataQueryDidUpdateNotification object:nil];
// Listen for the first phase gathering
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(queryIsDoneGathering:) name:NSMetadataQueryDidFinishGatheringNotification
object:nil];
And I also realize that you can get things like upload and download progress during the NSMetadataQueryDidUpdateNotification, however this update method doesn't seem to provide very reliable results all the time.
I was wondering if there is any way in which you can use an actual callback method to notify you when a file has been uploaded to iCloud? Or is the NSMetadataQueryDidUpdateNotification method the standard way of doing so?
Any hints and help would be appriciated :)
Thank you for your time :)