Qbyte
Qbyte

Reputation: 13283

Error when performing a `NSMetadataQuery` in iCloud Drive

I get the following error when performing a NSMetadataQuery in iCloud Drive:

[ERROR] cannot query iCloud Drive items: Error Domain=BRCloudDocsErrorDomain Code=16 "Sync is restricted for this client" UserInfo={NSDescription=Sync is restricted for this client}

This might be my problem

I have a free developer account. However I have not found anything about a requirement to use a paid developer account for using NSMetadataQuery in iCloud Drive.

Note that I found out that one has to use a paid developer account to use the iCloud Key-Value and Document Storage which is not the same as iCloud Drive.

What I tried

I used the following code to make the query (calling Test().getAllFilesIniCloud()):

class Test {

    let metadataQuery = NSMetadataQuery()
    func getAllFilesIniCloud() {
        metadataQuery.searchScopes = [
            NSMetadataQueryUbiquitousDocumentsScope,
            NSMetadataQueryUbiquitousDataScope,
            NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope,
        ]
        let predicate = NSPredicate { (any, dict) -> Bool in
            print(any as Any, dict as Any)
            return false
        }
        metadataQuery.predicate = predicate
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(self.queryDidFinishGathering(_:)),
            name: .NSMetadataQueryDidFinishGathering,
            object: metadataQuery)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(self.queryDidStart(_:)),
            name: .NSMetadataQueryDidStartGathering,
            object: metadataQuery)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(self.queryDidUpdate(_:)),
            name: .NSMetadataQueryDidUpdate,
            object: metadataQuery)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(self.queryGathering(_:)),
            name: .NSMetadataQueryGatheringProgress,
            object: metadataQuery)

        metadataQuery.enableUpdates()
        if metadataQuery.start() {
            print("query did start")
        }
    }

    @objc func queryGathering(_ notification: NSNotification) {
        print("gathering")
        self.getValuesOf(notification: notification)
    }
    @objc func queryDidStart(_ notification: NSNotification) {
        print("didStart")
        self.getValuesOf(notification: notification)
    }
    @objc func queryDidUpdate(_ notification: NSNotification) {
        print("didUpdate")
        self.getValuesOf(notification: notification)
    }

    @objc func queryDidFinishGathering(_ notification: NSNotification) {
        print("didFinish gathering")
        metadataQuery.disableUpdates()
        metadataQuery.stop()
        self.getValuesOf(notification: notification)
    }

    func getValuesOf(notification: NSNotification) {

        let metadataQuery = notification.object as! NSMetadataQuery

        metadataQuery.disableUpdates()
        for item in metadataQuery.results as! [NSMetadataItem] {

            let url = item.value(forAttribute: NSMetadataItemURLKey) as! URL
            print(url)
        }

        metadataQuery.enableUpdates()
    }
}

When starting the query it prints and nothing else:

didStart
... [default] [ERROR] cannot query iCloud Drive items: Error Domain=BRCloudDocsErrorDomain Code=16 "Sync is restricted for this client" UserInfo={NSDescription=Sync is restricted for this client}
query did start

Upvotes: 4

Views: 733

Answers (1)

Stefan
Stefan

Reputation: 300

You need to add iCloud to Capability in your Project and select key-value + iCloud Documents + add your container. I had have the same problem.

Upvotes: 1

Related Questions