如何告诉iOS从iCloud Drive下载文件并获得进度反馈

Wes*_*ton 7 cocoa objective-c icloud ios8

我正在使用UIDocumentPicker来选择文件,但如果它很大,则可能需要一段时间才能打开,这对用户来说并不是特别好的体验.

我看过Apple的iCloud编程指南,我似乎无法弄清楚如何实际下载文件并获得一些进度反馈,文档太模糊了.我知道我应该对NSMetadataItems做一些事情,但实际上没有太多解释如何获得并使用它.

有人可以向我解释一下吗?

PS可以用比我更高的代表用UIDocumentPicker和iCloudDrive标记这篇文章?

HiD*_*Deo 9

据我所知,您只能使用Ubiquitous Item Downloading Status Constants检索进度反馈,该 常量只提供3种状态:

  • NSURLUbiquitousItemDownloadingStatusCurrent
  • NSURLUbiquitousItemDownloadingStatusDownloaded
  • NSURLUbiquitousItemDownloadingStatusNotDownloaded

因此,您无法获得量化的进度反馈,只有部分也可以下载.

为此,您需要准备并启动NSMetadataQuery,添加一些观察者并使用NSURLUbiquitousItemDownloadingStatusKey键检查NSMetadataItem的下载状态.

self.query = [NSMetadataQuery new];
self.query.searchScopes = @[ NSMetadataQueryUbiquitousDocumentsScope ];
self.query.predicate = [NSPredicate predicateWithFormat:@"%K like '*.yourextension'", NSMetadataItemFSNameKey];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidUpdate:) name:NSMetadataQueryDidUpdateNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:nil];

[self.query startQuery];
Run Code Online (Sandbox Code Playgroud)

然后,

- (void)queryDidUpdate:(NSNotification *)notification
{
    [self.query disableUpdates];

    for (NSMetadataItem *item in [self.query results]) {
        NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
        NSError *error = nil;
        NSString *downloadingStatus = nil;

        if ([url getResourceValue:&downloadingStatus forKey:NSURLUbiquitousItemDownloadingStatusKey error:&error] == YES) {
            if ([downloadingStatus isEqualToString:NSURLUbiquitousItemDownloadingStatusNotDownloaded] == YES) {
                // Download
            }
            // etc.
        }
    }

    [self.query enableUpdates];
}
Run Code Online (Sandbox Code Playgroud)