【问题标题】:Core Spotlight in Parse Objects解析对象中的核心聚焦
【发布时间】:2015-10-14 01:36:59
【问题描述】:

我的应用程序使用放入 PFTableViewController 的 Parse Objects。该应用程序的用户可以添加新内容,并且对于使用该应用程序的每个其他人来说,这将显示为一个新单元格。我想在此实现 iOS 9 的 Core Spotlight 功能,以便当用户打开应用程序时,它会索引来自 PFObjects 的数据。例如,应用程序(祈祷应用程序)中的用户说他们需要为成为酒鬼而祈祷……其他人可以在电话上转到 Spotlight 搜索,输入酒鬼,然后会显示该特定请求。我在 App Search Programming Guide 中看到的类似这样的例子是用 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.")
   }
}

【问题讨论】:

    标签: ios search ios9 corespotlight


    【解决方案1】:

    首先,不需要在 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 循环,并使其非常动态地接收每个单元格/行的信息。

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

    就打开应用程序而言,有一种方法可以在您的 AppDelegate 中使用,您可以在其中使用 NSUserActivity,通过它的用户信息或您想要的方式来获取从那里打开某个页面的信息。

    -(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]}];
    }
    

    然后使用“mutArray”进行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"); }];
    

    【讨论】:

    • 我知道如何创建它,我的更多问题是关于如何循环遍历 PFObjects。
    猜你喜欢
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    相关资源
    最近更新 更多