ios - 在 iCloud Drive 中执行 `NSMetadataQuery` 时出错

标签 ios icloud icloud-drive nsmetadataquery

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

[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}

这可能是我的问题

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

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

我尝试了什么

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

最佳答案

你需要在你的项目中添加iCloud到Capability,然后选择key-value + iCloud Documents + 添加你的容器。我遇到了同样的问题。

关于ios - 在 iCloud Drive 中执行 `NSMetadataQuery` 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57380800/

相关文章:

iphone - 简单的iCloud实现

ios - Dropbox UIDocumentPicker 插件无法打开 OS X 文件包

ios - 如果我不启用 iCloud 备份,我是否必须排除我的文档目录以防止文件备份到 iCloud 和 iTunes?

ios - UIDocumentPickerViewController:如何打开给定的子目录

java - 使用 appium 捕获 Android 屏幕截图

ios - 在选项卡式应用程序中更改选项卡后运动功能不起作用

objective-c - UIColor 颜色WithPatternImage : transparency issues in UITableView's separatorColor

安卓同步API?

ios - 如何使用 Swift 中的 Cognito 用户池注册用户并验证他们的电话号码?

iOS App 不会使用自定义 iCloud 容器