NSUserActivity 未被 Spotlight 索引

NSUserActivity Not Getting Indexed by Spotlight

我试图让我的 NSUserActivity 被 iOS 中 Spotlight 中的私有索引索引。我已按照 Apple's Index Activities and Navigation Points 指南中的所有步骤进行操作,但我的 activity 似乎根本没有被聚光灯索引。

指南说:

To guarantee that the activity and its metadata get indexed, you must hold a strong reference to the activity until it gets added to the index. There are two ways to do this: The first way is to assign the activity to a property in the controller object that creates the activity. The second way is to use the userActivity property of the UIResponder object.

我选择了第一个选项(在我的视图控制器中创建一个 属性 来保存 NSUserActivity)。

var lastSearchedUserActivity: NSUserActivity?

这个想法是,当用户搜索某物时,他最后的查询会在设备上编入索引。我有以下方法准备用户 activity 并(假设)索引它:

func prepareLastSearchedUserActivity(tags: [String], server: Server) {
    if Settings.applicationIndexedUserActivitiesAsShortcutTypes.contains(.LastSearched) {
        print("Get ready to index. and tags \(tags.reduce("") { "\([=11=]) \()" })")
        let activity = NSUserActivity(activityType: ShortcutType.LastSearched.rawValue)

        activity.title = server.serverName
        activity.userInfo = ["tags": tags, "server name": server.serverName]

        let attributeSet = CSSearchableItemAttributeSet()
        attributeSet.contentDescription = tags.reduce("") { "\([=11=]) \()" }
        attributeSet.relatedUniqueIdentifier = ShortcutType.LastSearched.rawValue

        activity.contentAttributeSet = attributeSet
        activity.keywords = Set(tags)
        activity.eligibleForSearch = true
        activity.eligibleForHandoff = false
        activity.becomeCurrent()
        self.lastSearchedUserActivity = activity
        //self.lastSearchedUserActivity?.becomeCurrent()
    }
}

调用此方法没有问题,但无法搜索 activity:我尝试使用分配给它的 titlekeywords 在 Spotlight 中进行搜索。 activity 永远不会出现。

我尝试过很多解决方案,包括:

据我所知,我正在做 Apple 在他们的指南中所做的一切。我完全迷路了。

我正在 iPhone 6S+ 上进行测试,因此可以使用 Spotlight 索引。控制台也不会打印任何与 Spotlight 相关的内容。

更新:

我只是将 activity 的委托设置为 self 并实现了 userActivityWillSave 方法。

根据NSUserActivityDelegate documentation,大约userActivityWillSave:

Notifies the delegate that the user activity will be saved to be continued or persisted.

因此正在调用此委托方法,但找不到索引项。这是更新后的代码:

func prepareLastSearchedUserActivity(tags: [String], server: Server) {
    if Settings.applicationIndexedUserActivitiesAsShortcutTypes.contains(.LastSearched) {
        print("Get ready to index. and tags \(tags.reduce("") { "\([=12=]) \()" })")
        let activity = NSUserActivity(activityType: ShortcutType.LastSearched.rawValue)

        activity.title = server.serverName
        activity.userInfo = ["tags": tags, "server name": server.serverName]
        activity.delegate = self

        let attributeSet = CSSearchableItemAttributeSet()
        attributeSet.contentDescription = tags.reduce("") { "\([=12=]) \()" }
        attributeSet.relatedUniqueIdentifier = ShortcutType.LastSearched.rawValue

        activity.contentAttributeSet = attributeSet
        activity.keywords = Set(tags)
        activity.eligibleForSearch = true
        activity.eligibleForHandoff = false
        self.lastSearchedUserActivity = activity
        activity.becomeCurrent()
        //self.lastSearchedUserActivity?.becomeCurrent()
    }
}

func userActivityWillSave(userActivity: NSUserActivity) {
    print("Yep it will save")
}

原来在属性集中设置 relatedUniqueIdentifier 会导致丢弃 userInfo 字典。

我把这行注释掉了:

attributeSet.relatedUniqueIdentifier = ShortcutType.LastSearched.rawValue

它现在按预期工作。尽管我需要找到另一种方法来从索引中删除我的用户活动。

更新:更多信息

如果您必须为您的 activity 使用唯一标识符,那么您需要先使用 Core Spotlight 对其进行索引,然后再对 NSUserActivity 本身进行索引。如果您尝试使用 relatedUniqueIdentifier 而不是首先在 Spotlight 中使用它,则 NSUserActivity 将不会被索引。这就是我的问题。

来自relatedUniqueIdentifier property documentation in the CSSearchableItemAttributeSet Class Reference

If you’re using both NSUserActivity and Core Spotlight APIs to index the same item, set this property in the activity to specify the unique identifier of the Core Spotlight item to which the activity is related, and to avoid displaying duplicate results in Spotlight.

If the unique identifier to which the activity is related hasn’t already been indexed with Core Spotlight, the activity won’t be indexed.