在 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/