【问题标题】:NSUserActivity Not Getting Indexed by SpotlightNSUserActivity 没有被 Spotlight 索引
【发布时间】:2016-05-08 15:09:51
【问题描述】:

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

指南说:

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

我选择了第一个选项(在我的视图控制器中创建一个属性来保存 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()
    }
}

调用此方法没有问题,但该活动不可搜索:我尝试使用分配给它的 titlekeywords 在 Spotlight 中搜索。该活动从未出现。

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

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

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

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

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

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

更新:

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

根据NSUserActivityDelegate documentation,约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")
}

【问题讨论】:

    标签: ios ios9 spotlight nsuseractivity ios-searchapi


    【解决方案1】:

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

    我把这行注释掉了:

    attributeSet.relatedUniqueIdentifier = ShortcutType.LastSearched.rawValue
    

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

    更新:更多信息

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

    来自relatedUniqueIdentifier property documentation in the CSSearchableItemAttributeSet Class Reference

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

    如果活动相关的唯一标识符没有 已被 Core Spotlight 索引,该活动将不会 索引。

    【讨论】:

    • 这对我也有用,但现在我不确定NSUserActivity 是否在保留relatedUniqueIdentifier 时被公开索引,因此它们与Core Spotlight 项目匹配。有谁知道或者有办法搜索公共索引吗?
    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2020-02-10
    • 1970-01-01
    相关资源
    最近更新 更多