将 CSSearchableItem 添加到 Core Spotlight (MacOS) 时出现未知错误

Unknown Error when adding an CSSearchableItem to Core Spotlight (MacOS)

我最近想使用 Core Spotlight 为我的一个项目中的搜索提供支持。但是,每当我将 CSSearchableItem 添加到 SearchIndex 时,我都会在完成处理程序中收到一个错误,描述如下:

The operation couldn’t be completed. (CSIndexErrorDomain error -1.)

根据 Apple 的参考资料,错误代码 -1 指的是 Unknown Error,这并不是很有帮助。我在我的应用程序中同时添加了 CoreSpotlight 和 CoreServices 框架,但我真的不知道我可能做错了什么。

我整理了一个最小的例子:

import Foundation
import CoreSpotlight

print("Start indexing...")
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
attributeSet.title = "test element"
attributeSet.contentDescription = "This is a description."
attributeSet.keywords =  ["test1", "test2", "test3"]
let item = CSSearchableItem(uniqueIdentifier: "123455", domainIdentifier: "TestDomain", attributeSet: attributeSet)
var ready = false
CSSearchableIndex.default().indexSearchableItems([item]) { (error) in
    if error == nil {
        print("Success")
    } else {
        print(error?.localizedDescription)
    }
    ready = true
}
//Wait for the block to finish
while (ready == false) {
    sleep(1)
}
print("Finish indexing...")

我设法从 WWDC17 编译了 Apple 的 Core Spotlight 示例项目,它确实可以正常运行。但是,我无法通过系统范围的 Spotlight 搜索获得索引项目。

有谁知道可能会出现什么问题?顺便说一下,我是 运行 最新的 High Sierra 版本。

[编辑]刚看到,其实还有其他人有这个问题。但是,问题尚未得到解答:Error while using CoreSpotlight

[Edit2] 更新到 10.13.2 后,行为发生了变化。放在操场上的这段代码现在正在运行;但是,放入我的应用程序中的相同代码仍然会产生错误,但它确实包含更多信息。打印错误对象导致:

Error Domain=CSIndexErrorDomain Code=-1003 "(null)" 
UserInfo={NSUnderlyingError=0x60000105f6e0 
{Error Domain=NSCocoaErrorDomain Code=4097 
"Couldn’t communicate with a helper application."}}

正如它向我显示的那样,这显然是框架中的一个错误,或者您对此有何看法?

最终我设法以某种方式解决了这个问题,我遵循了代码在操场上工作的提示。所以我还是做了我想做的事,把代码放在一个框架里。 没有任何进一步的改变,它开始工作。所以显然它与项目配置有关,但如果有人想出 Spotlight 失败的根本原因是什么,那就太好了。

在 macOS 10.13 中,SIP 和 App Sandbox 似乎变得更加严格了。 经过大量挖掘以完成这项工作,这就是我所做的,并且应该也适用于阅读本文的其他人:

  1. 在 Xcode 中,为您的 CoreSpotlight 代码 运行 的目标切换 "App Sandbox" 功能。如果这是在框架中,则它需要是托管应用程序的目标。这对于新项目来说应该不是必需的(有关详细信息,请参阅 this bug report)。
  2. 为 application/binary 启用开发签名,这将 运行 CoreSpotlight 代码。就我而言,它是测试主机。