如何验证iCloud中文件的存在? [英] How do I verify a file's existence in iCloud?

查看:151
本文介绍了如何验证iCloud中文件的存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道该文件存在,因为我可以下载它,但是我需要检查它是否存在.我尝试使用

I know that the file exists, because I can download it, but I need to check to see whether it exists. I have tried using

[NSFileManager contentsOfDirectoryAtPath:error:]

但是它给了我空值.我不明白为什么会这样,因为我仍然可以下载所需的文件.也许这是一个不正确的URL,但是我正在使用的URL是我在寻找所需的UIDocument创建时打印的URL.也许我使用了错误的方法?

but it gives me null. I don't understand why that is because I can still download the files that I'm looking for. Maybe it's an incorrect URL, but the URL that I'm using is the one that I printed at creation of my UIDocument that I'm looking for. Maybe I'm using the wrong method?

我也尝试过使用NSMetadataQuery,我可以得到它来回送通知,但是即使我可以显式下载我要查找的文件,它也永远不会产生结果.

I have also tried using NSMetadataQuery, and I can get it to give back notifications, but it doesn't ever have results even though I can explicitly download the files I'm looking for.

推荐答案

要在iCloud中查找文件,请使用NSMetadataQuery.这样既可以找到已下载的文件,也可以找到用户帐户中但尚未下载到本地设备的文件.使用NSFileManager最多只能找到已经下载的文件.

To find files in iCloud, you use NSMetadataQuery. That will find both files that have already been downloaded as well as files that are in the user's account but which haven't been downloaded to the local device yet. Using NSFileManager will, at best, only find files that have already been downloaded.

您使用类似这样的设置:

You set it up with something like this:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[self setMetadataQuery:query];
[query setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]];
[query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*'", NSMetadataItemFSNameKey]];

您将要观察NSMetadataQueryDidStartGatheringNotificationNSMetadataQueryDidUpdateNotification,甚至可能是NSMetadataQueryDidFinishGatheringNotification.然后开始查询:

You'll want to observe NSMetadataQueryDidStartGatheringNotification, NSMetadataQueryDidUpdateNotification, and probably NSMetadataQueryDidFinishGatheringNotification. Then start the query:

[query startQuery];

完成此操作后,当查询发现iCloud文件时,您将收到通知.通知中将包含NSMetadataItem的实例,您可以使用这些实例获取文件大小,下载状态等信息.

With that done, you'll get notifications as the query discovers iCloud files. The notifications will include instances of NSMetadataItem, which you can use to get information like file size, download status, etc.

这篇关于如何验证iCloud中文件的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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