解析对象中的核心聚焦

时间:2015-09-23 19:04:07

标签: ios search ios9 corespotlight

我的应用程序使用放入PFTableViewController的Parse对象。该应用的用户可以添加新内容,并且对于使用该应用的其他每个人,该内容将显示为新单元格。我想在此实现iOS 9的Core Spotlight功能,这样当用户打开应用程序时,它会对来自PFObjects的数据进行索引。例如,应用程序中的用户(祈祷应用程序)说他们需​​要祈祷成为酗酒者...其他人可以通过电话进入Spotlight搜索,键入酒精,并且特定请求会显示出来。我在应用搜索编程指南中看到的这样的例子都是用Swift编写的,而我在Obj-C中更舒服,宁愿不把这个应用程序的整个类重写到Swift只是为了这个特征。它也适用于单一用途的项目,有人可以指导我转换为Obj-C,以及如何让它在可见的时间内对所有20个进行索引,并在它们向下翻页时继续索引?

// Create an attribute set to describe an item.
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeData as String)
// Add metadata that supplies details about the item.
attributeSet.title = "July Report.Numbers"
attributeSet.contentDescription = "iWork Numbers Document"
attributeSet.thumbnailData = DocumentImage.jpg

// Create an item with a unique identifier, a domain identifier, and the attribute set you created earlier.
let item = CSSearchableItem(uniqueIdentifier: "1", domainIdentifier: "file-1", attributeSet: attributeSet)

// Add the item to the on-device index.
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([item]) { error in
   if error != nil {
      print(error?.localizedDescription)
   }
   else {
      print("Item indexed.")
   }
}

1 个答案:

答案 0 :(得分:0)

首先,不需要在Swift中执行,有Obj-C代码和文档。

示例

CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*)kUTTypeJSON];

attributeSet.title = title;
attributeSet.contentDescription = description;
attributeSet.keywords = @[keywords];
attributeSet.thumbnailData = thumbnailData;

CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:identifier domainIdentifier:domainIdentifier attributeSet:attributeSet];

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) {
    NSLog(@"Indexed");
}];

我认为他们想要解决这个问题的方法是在完成加载/显示Parse信息之后调用一个函数(即基本上任何时候你的PFTableViewController的表视图都被更新)

您必须执行for循环并使其非常动态以接收每个单元格/行的信息。

就核心聚光灯搜索中显示的内容而言,您必须为每个项目设置关键字。请注意,关键字区分大小写,在我注意到之前花了一些时间。这些关键字将与该单元格/行匹配。

至于打开应用程序,有一种方法可以在您使用 NSUserActivity 的AppDelegate中使用,或者您想要使用它,并从那里获取信息以打开某个页面。

-(BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * _Nullable))restorationHandler

希望这有帮助!让我知道我是否可以做任何其他事情来使这一点更加清晰,因为我知道我并不是最擅长使事情变得简洁。快乐的编码!

在此处回复@ user717452评论以获得更好的样式

你刚才提到了swift,所以我展示了聚光灯搜索的目标c代码。

你可以循环遍历你的PFObjects,假设它是一个PFObjects数组,只需做一个常规for循环和一个带有字典的可变数组。假设你的PFObject是这样的:parse.com/docs/ios/api/Classes/PFObject.html

NSMutableArray *mutArray = [[NSMutableArray alloc] init];
for(int i=0; i<pfobjectArray.count;i++ {
   [mutArray addObject:@{@"title":[pfobjectArray[i] title], @"keywords":[pfobjectArray[i] keywords], @"thumbnail_data":[pfobjectArray[i] thumbnail], @"identifier":[pfobjectArray[i] identifier]}];
}

然后使用&#34; mutArray&#34;对于spotlightSearch。

for(int i=0; i<mutArray.count;i++) {


   CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*)kUTTypeJSON];
   attributeSet.title = mutArray[i][@"title"];
   attributeSet.contentDescription = mutArray[i][@"description"];
   attributeSet.keywords = [NSArray arrayWithArray:mutArray[i][@"keywords"]];
   attributeSet.thumbnailData = mutArray[i][thumbnail_data];

   CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:mutArray[i][@"identifier"] domainIdentifier:@"com.iphone.app" attributeSet:attributeSet];
      [arrayOfItems addObject:item];
   }

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:[arrayOfItems mutableCopy] completionHandler: ^(NSError * __nullable error { NSLog(@"Spotlight Popular"); }];