我试图让我的NSUserActivity被iOS中的Spotlight中的私有索引索引。我遵循了苹果的索引活动和导航点指南中的所有步骤,但我的活动似乎完全没有被聚光灯所索引。
指南说:
要确保活动及其元数据被索引,必须保持对该活动的强引用,直到将其添加到索引中为止。有两种方法可以做到这一点:第一种方法是将活动分配给创建活动的控制器对象中的属性。第二种方法是使用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()
}
}调用此方法时不存在任何问题,但该活动是不可搜索的:我尝试使用分配给它的title和keywords在Spotlight中搜索。活动从来没有出现过。
我尝试过许多解决办法,包括:
eligibleForSearch调用。苹果的指南没有直接提到这一点,但提供的链接中的代码片段似乎意味着将该行设置为true应该自动将活动添加到索引中。becomeCurrent(),而是说它将为您调用(如何调用?)不知道)。不管你怎么看,我试着自己打电话给它。在把它分配给我的财产后,我也试着调用它。没有骰子。苹果确实表示,当在一个以eligibleForSearch作为true的活动上调用eligibleForSearch时,它将被添加到索引中。userActivity属性来创建和配置该活动。因为我使用的是提供的属性,所以它不应该提前释放。据我所知,我所做的一切都是苹果在他们的指导下所做的。我完全迷路了。
我正在iPhone 6S+上进行测试,这样就可以使用Spotlight索引了。控制台也不打印与Spotlight相关的任何内容。
更新:
我只是将活动的委托设置为self,并实现了userActivityWillSave方法。
根据文档的说法,关于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")
}发布于 2016-01-31 01:36:07
结果是,在属性集中设置relatedUniqueIdentifier将导致userInfo字典被丢弃。
我评论了这句话:
attributeSet.relatedUniqueIdentifier = ShortcutType.LastSearched.rawValue现在,它正在如预期的那样运作。不过,我需要找到另一种方法从索引中删除我的用户活动。
更新:更多信息
如果您必须为您的活动使用唯一标识符,那么您需要首先用Core对其进行索引,然后对NSUserActivity本身进行索引。如果您尝试使用relatedUniqueIdentifier而不首先在Spotlight中使用它,则不会对NSUserActivity进行索引。这就是我的问题所在。
来自类引用
如果同时使用NSUserActivity和Core对同一项进行索引,请在活动中设置此属性,以指定与活动相关的Core项的唯一标识符,并避免在Spotlight中显示重复的结果。 如果与活动相关的唯一标识符尚未用Core索引,则该活动将不会被索引。
https://stackoverflow.com/questions/35108977
复制相似问题