【问题标题】:Error when performing a `NSMetadataQuery` in iCloud Drive在 iCloud Drive 中执行“NSMetadataQuery”时出错
【发布时间】:2019-12-14 06:54:48
【问题描述】:

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

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

这可能是我的问题

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

请注意,我发现必须使用付费开发者帐户才能使用与 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

【问题讨论】:

    标签: ios icloud icloud-drive nsmetadataquery


    【解决方案1】:

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

    【讨论】:

    • 同上。虽然它不需要是键值。 iCloud 文档也可以。
    猜你喜欢
    • 1970-01-01
    • 2018-04-21
    • 2015-11-17
    • 2011-12-18
    • 1970-01-01
    • 2016-08-23
    • 2013-12-16
    • 2016-02-16
    • 1970-01-01
    相关资源
    最近更新 更多