I am struggling with odd behavior I encounter when I'm trying to open a UIDocument from iCloud that has been created using another device. When I call openWithCompletionHandler it never completes.
Here's how I reproduce the behavior:
- I created a new UIDocument on my iPad and stored it in iCloud
- Then I removed the same app, but from iPhone
- I run the app from XCode on my iPhone
- So far the iPhone is clean because the app was removed
- When the app runs on iPhone for the first time, it calls openWithCompletionHandler method which hangs and nothing is happened. It doesn't reach a block with code to execute after the openWithCompletionHandler operation concludes.
It works only when I create a new UIDocument and store it in iCloud from iPhone. Then if I do the same steps (1-5) but on iPad I encounter a similar behavior.
Here is the source code that I have:
if ([query resultCount] == 1)
{
NSMetadataItem *item = [query resultAtIndex:0];
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
_progressDoc = [[ProgressDocument alloc] initWithFileURL:url];
[_progressDoc openWithCompletionHandler:^(BOOL success) {
if (success)
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(progressDocumentHasChanged:)
name:UIDocumentStateChangedNotification
object:nil];
NSLog(@"Progress Document opened from iCloud");
}
else {
NSLog(@"failed to open Progress Document from iCloud");
}
}];
}
Has anybody encountered a similar issue?
I also tried to download the file manually using the method below, but still same behavior...
- (BOOL)downloadFileIfNotAvailable:(NSURL*)file {
NSNumber* isIniCloud = nil;
if ([file getResourceValue:&isIniCloud forKey:NSURLIsUbiquitousItemKey error:nil]) {
// If the item is in iCloud, see if it is downloaded.
if ([isIniCloud boolValue]) {
NSNumber* isDownloaded = nil;
if ([file getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil]) {
if ([isDownloaded boolValue])
return YES;
// Download the file.
NSError *error;
NSFileManager* fm = [NSFileManager defaultManager];
BOOL success = [fm startDownloadingUbiquitousItemAtURL:file error:&error];
if (success) {
NSLog(@"Started download at URL: %@", file);
} else {
NSLog(@"Failed to start download at URL: %@: %@", file, error.localizedDescription);
}
return NO;
}
}
}
// Return YES as long as an explicit download was not started.
return YES;
}
Thanks a lot!