6

我知道在 iOS 上您只能NSMetadataQuery用于 iCloud 文件。但是,根据 Apple 的指南,您仍然可以NSMetadataItem通过手动搜索所需的文件来获取FileWrapper

iOS 允许在 iCloud 中搜索元数据以查找文件对应的文件。它只提供了Objective-C 接口来查询文件元数据,NSMetadataQuery并且NSMetadataItem只支持搜索iCloud 的搜索范围。

与桌面不同,iOS 应用程序的沙箱不能使用元数据类进行搜索。为了搜索应用程序的沙箱,您需要使用NSFileManager该类递归地遍历沙箱文件系统中的文件。一旦找到匹配的文件,您就可以按照您需要的方式访问该文件。您还可以使用NSMedatataItem该类来检索该特定文件的元数据。

但是,我找不到NSMetadataItem从 iOS 沙盒上的本地文件获取的方法。您可以从文件 URL 进行初始化NSMetadataItem,但这仅在 macOS 上可用。FileManager似乎也没有可以从中检索的 API NSMetadataItem

如何获取NSMetadataItemiOS App Sandbox 上的本地文件?

4

1 回答 1

0

用于NSMetadataQuery查询 iCloud 文件,添加观察者获取通知。

metaDataQuery.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope, NSMetadataQueryUbiquitousDataScope]

NotificationCenter.default.addObserver(self, selector: #selector(self.metadataQueryDidFinishGathering),
name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: metaDataQuery)
metaDataQuery.start()
func metadataQueryDidFinishGathering(notification: NSNotification) {
    let result: [NSMedatataItem] = (notification.object as! NSMetadataQuery).results as! [NSMedatataItem]
    // handle result
}
于 2020-05-14T10:19:00.080 回答