ios - NSUserActivity 未被 Spotlight 索引

标签 ios ios9 spotlight nsuseractivity ios-searchapi

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

指南说:

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.

我选择了第一个选项(在我的 View Controller 中创建一个属性来保存 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 的指南没有直接说明这一点,但提供的链接中的代码片段似乎暗示将此行设置为 true 应该会自动将事件添加到索引中。

    <
  • Apple 并没有说应该调用 becomeCurrent(),而是会为您调用它(如何调用?不知道)。不管你怎么看,我试着自己调用它。在将它分配给我的属性(property)后,我也尝试调用它。没有骰子。 Apple 确实说过,当在 eligibleForSearchtrue 的事件上调用 becomeCurrent() 时,它将被添加到索引中。

  • 我什至直接使用 View Controller 的 userActivity 属性来创建和配置事件。因为我正在使用提供的属性,所以它不应该提前解除分配。

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

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

更新:

我只是将事件的委托(delegate)设置为 self 并实现了 userActivityWillSave 方法。

根据NSUserActivityDelegate documentation , 关于 userActivityWillSave:

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

所以调用了这个委托(delegate)方法,但是找不到索引项。这是更新后的代码:

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")
}

最佳答案

事实证明,在属性集中设置 relatedUniqueIdentifier 会导致 userInfo 字典被丢弃。

我把这一行注释掉了:

attributeSet.relatedUniqueIdentifier = ShortcutType.LastSearched.rawValue

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

更新:更多信息

如果您必须为您的事件使用唯一标识符,那么您需要先使用 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.

关于ios - NSUserActivity 未被 Spotlight 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108977/

相关文章:

ios - 是否可以在 iPadOS 上渲染 HDMI 视频?

ios - |-(20)-[UIInputSetContainerView 约束在调用状态栏可见时中断

重新安装后的 iOS 推送通知设置

macos - 使用 Coredata 进行创纪录水平的 Spotlight 索引

macos - Mac OS X 的 Spotlight 可以配置为忽略某些文件类型吗?

ios - 如何删除 UIViewController 类的 xib 文件?

ios - Swift - 有 "Dictionary With Duplicate Keys"解决方法吗?

iphone - 如何检测哪个 3rd sdk 使用 UDID?

ios - 获取 Unfollower Instagram api swift

ios - 链接到 API 的 Spotlight IOS