如何告诉iOS从iCloud Drive下载文件并获取进度反馈 [英] How do I tell iOS to download a file from iCloud Drive and get progress feedback

查看:1738
本文介绍了如何告诉iOS从iCloud Drive下载文件并获取进度反馈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我看了苹果的iCloud编程指南,我似乎不知道如何实际下载文件,并得到一些进度反馈,文档太模糊了。我知道我应该做一些与NSMetadataItems,但是没有太多解释实际上如何获取和使用它。

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?

PS有人可能有更高的代表谁标记这篇文章与UIDocumentPicker和iCloudDrive?

P.S. can someone with higher rep than me tag this post with UIDocumentPicker and iCloudDrive?

推荐答案

据我所知,你只能检索进度反馈使用
无处不在的项目下载状态常量,只提供3种状态:

To my knowledge, you can only retrieve progress feedback using the Ubiquitous Item Downloading Status Constants which provides only 3 states:


  • NSURLUbiquitousItemDownloadingStatusCurrent

  • NSURLUbiquitousItemDownloadingStatusDownloaded

  • NSURLUbiquitousItemDownloadingStatusNotDownloaded

所以你不能有量化的进度反馈, aka下载与否。

So you can't have a quantified progress feedback, only partial aka downloaded or not.

要执行此操作,您需要准备并启动 NSMetadataQuery ,添加一些观察者并检查您的 NSURLUbiquitousItemDownloadingStatusKey 键

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];

然后,

- (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];
}

这篇关于如何告诉iOS从iCloud Drive下载文件并获取进度反馈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆