5

我试图让我的 NSUserActivity 被 iOS 中 Spotlight 中的私有索引索引。我已按照Apple 的索引活动和导航点指南中的所有步骤操作,但我的活动似乎根本没有被聚光灯索引。

该指南说:

为了保证活动及其元数据被索引,您必须持有对该活动的强引用,直到它被添加到索引中。有两种方法可以做到这一点: 第一种方法是将活动分配给创建活动的控制器对象中的属性。第二种方法是使用 UIResponder 对象的 userActivity 属性。

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

var lastSearchedUserActivity: NSUserActivity?

这个想法是,当用户搜索某些东西时,他的最后一个查询会在设备上被索引。我有以下方法可以准备用户活动并(据说)对其进行索引:

func prepareLastSearchedUserActivity(tags: [String], server: Server) {
    if Settings.applicationIndexedUserActivitiesAsShortcutTypes.contains(.LastSearched) {
        print("Get ready to index. and tags \(tags.reduce("") { "\($0) \($1)" })")
        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("") { "\($0) \($1)" }
        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()
    }
}

调用此方法没有问题,但该活动不可搜索:我已尝试使用title分配给它的 Spotlight 进行搜索,而keywords. 该活动从未出现。

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

  • 创建活动后直接移动eligibleForSearch呼叫。Apple 的指南没有直接说明这一点,但提供的链接中的代码片段似乎暗示将这一行设置为true应该自动将活动添加到索引中。

  • 苹果并没有说becomeCurrent()应该调用它,而是说它会为你调用(如何?不知道)。不管你看到什么,我都试着自己打电话。在将其分配给我的财产后,我也尝试调用它。没有骰子。苹果确实说过,当调用becomeCurrent()具有eligibleForSearchas的活动时true,它将被添加到索引中。

  • 我什至userActivity直接使用视图控制器的属性来创建和配置活动。因为我正在使用提供的属性,所以它不应该提前释放。

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

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

更新:

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

根据NSUserActivityDelegate文档,关于userActivityWillSave:

通知代理用户活动将被保存以继续或持久。

所以这个委托方法被调用了,但是索引项却无处可寻。这是更新的代码:

func prepareLastSearchedUserActivity(tags: [String], server: Server) {
    if Settings.applicationIndexedUserActivitiesAsShortcutTypes.contains(.LastSearched) {
        print("Get ready to index. and tags \(tags.reduce("") { "\($0) \($1)" })")
        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("") { "\($0) \($1)" }
        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")
}
4

1 回答 1

6

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

我评论了这一行:

attributeSet.relatedUniqueIdentifier = ShortcutType.LastSearched.rawValue

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

更新:更多信息

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

来自Class ReferencerelatedUniqueIdentifier中的属性文档CSSearchableItemAttributeSet

如果您同时使用 NSUserActivity 和 Core Spotlight API 来索引同一个项目,请在活动中设置此属性以指定与活动相关的 Core Spotlight 项目的唯一标识符,并避免在 Spotlight 中显示重复的结果。

如果与活​​动相关的唯一标识符尚未使用 Core Spotlight 编制索引,则不会编制该活动的索引。

于 2016-01-31T01:36:07.927 回答