当我尝试从使用其他设备创建的iCloud打开UIDocument时,我遇到了奇怪的行为。当我打电话给 openWithCompletionHandler 时,它永远不会完成。
以下是我重现行为的方法:
仅当我创建一个新的UIDocument并将其存储在iPhone的iCloud中时,它才有效。然后,如果我执行相同的步骤(1-5),但在iPad上遇到类似的行为。
以下是我的源代码:
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");
}
}];
}
有没有人遇到过类似的问题?
我也尝试使用下面的方法手动下载文件,但仍有相同的行为...
- (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;
}
非常感谢!