2

NSMetadataQuery在 iCloud Drive 中执行时出现以下错误:

[错误] 无法查询 iCloud Drive 项目:错误域 = BRCloudDocsErrorDomain 代码 = 16“此客户端的同步受限” UserInfo={NSDescription=此客户端的同步受限}

这可能是我的问题

我有一个免费的开发者帐户。NSMetadataQuery但是,我还没有发现任何关于在 iCloud Drive 中使用付费开发者帐户的要求。

请注意,我发现必须使用付费开发者帐户才能使用与 iCloud Drive 不同的 iCloud Key-Value 和 Document Storage。

我试过的

我使用以下代码进行查询(调用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()
    }
}

开始查询时,它只打印,没有其他内容:

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
4

1 回答 1

1

您需要在项目中将 iCloud 添加到 Capability 并选择键值 + iCloud Documents + 添加您的容器。我有同样的问题。

于 2020-06-28T22:23:08.810 回答