I am using the UIDocumentPicker to select a file but if it's large it can take a while to open and that is not a particularly good experience for users.
I have looked at the iCloud programming guide from Apple and I cannot seem to figure out how to actually download a file and get some progress feedback, the documentation is just too vague. I know I am supposed to do something with NSMetadataItems, but there isn't much explaining actually how to get one and use it.
Can someone please explain it to me?
P.S. can someone with higher rep than me tag this post with UIDocumentPicker and iCloudDrive?
To my knowledge, you can only retrieve progress feedback using the Ubiquitous Item Downloading Status Constants which provides only 3 states:
So you can't have a quantified progress feedback, only partial aka downloaded or not.
To do so, you need to prepare and start your NSMetadataQuery, add some observers and check the downloading status of your NSMetadataItem using the NSURLUbiquitousItemDownloadingStatusKey key.
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];
Then,
- (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];
}