1

Using xcode 8 running on iOS 10. Trying to put together a super simple NSMetadataQuery. Waisted almost a day on this now. Managed to get this to do a query, but it doesn't return any documents from My iCloud, none.

class ViewController: UIViewController {

    var metadataQuery: NSMetadataQuery!
    var metadataQueryDidUpdateObserver: AnyObject?
    var metadataQueryDidFinishGatheringObserver: AnyObject?

    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view, typically from a nib.

        metadataQuery = NSMetadataQuery()

       metadataQuery?.predicate = NSPredicate(format: "NSMetadataItemFSNameKey == '*'")
        metadataQuery?.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope,                                         NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope]

        NotificationCenter.default.addObserver(self, selector: #selector(initalGatherComplete), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(queryStarted), name: NSNotification.Name.NSMetadataQueryDidUpdate, object: nil)

        metadataQuery.start()


    }

    func queryStarted(notification: NSNotification) {
        print("queryStarted")
    }

    func initalGatherComplete(notification: NSNotification) {
        metadataQuery.stop()
        metadataQuery.disableUpdates()

        let resultCounter = metadataQuery.resultCount
        print("%lu", resultCounter,metadataQuery.results)

        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

It returns nothing, no errors, no data, just vapid space. Now I yes I haven't saved any documents locally, but I am not interested in local docs I want iCloud docs.

My plist says this...

<key>NSUbiquitousContainers</key>
<dict>
    <key>iCloud.com.ch.cqd.iCloudQuery</key>
    <dict>
        <key>NSUbiquitousContainerIsDocumentScopePublic</key>
        <true/>
        <key>NSUbiquitousContainerName</key>
        <string>iCloudQuery</string>
        <key>NSUbiquitousContainerSupportedFolderLevels</key>
        <string>Any</string>
    </dict>
</dict>

What am I missing here?

user3069232
  • 8,587
  • 7
  • 46
  • 87

1 Answers1

0

Ok,

After much searching I think I found the answers. This app created its own iCloud subdirectory [with the same name] and if I copy files into it; I can see them. It works.

But I can only see files and not directories, a limitation of NSMetadataQuery it seems.

My investigations also took me to extensions, of which there are half a dozen or more it seems. One of those UIDocument extension, but wait this also only offers single files too. Better I can see folders within my generic icloud account, posted some code SO question that works here.

There is a means to identify a custom piece of code to open a specific file type, of which I followed and did this tutorial.

iOS Extensions: Document Provider Tutorial

It works well too, google it if this link breaks. But this is a single file in effect; and I wanted a folder.

Might this be the answer, perhaps. But its not really ideal.

Community
  • 1
  • 1
user3069232
  • 8,587
  • 7
  • 46
  • 87