6

I am aware that on iOS you can only use NSMetadataQuery for iCloud files. However, according to Apple's guide, you can still get NSMetadataItem by searching for the file you want manually with FileWrapper:

iOS allows metadata searches within iCloud to find files corresponding files. It provides only the Objective-C interface to file metadata query, NSMetadataQuery and NSMetadataItem, as well as only supporting the search scope that searches iCloud.

Unlike the desktop, the iOS application’s sandbox is not searchable using the metadata classes. In order to search your application’s sandbox, you will need to traverse the files within the sandbox file system recursively using the NSFileManager class. Once matching file or files is found, you can access that file in the manner that you require. You are also able to use the NSMedatataItem class to retrieve metadata for that particular file.

However, I couldn't find a way to get the NSMetadataItem from a local file on iOS sandbox. You can initialize NSMetadataItem from a file URL but that's available only on macOS. FileManager also doesn't seem to have an API to retrieve NSMetadataItem from it.

How do I get NSMetadataItem for local files on iOS App Sandbox?

Community
  • 1
  • 1
HuaTham
  • 7,486
  • 5
  • 31
  • 50

1 Answers1

0

Use NSMetadataQuery to query iCloud files, add observer to get notification.

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
}
doubleDai
  • 33
  • 7