2015-09-23 48 views
0

我的应用使用放入PFTableViewController的解析对象。该应用的用户可以添加新的内容,并且对于使用该应用的其他人而言,该内容将显示为新的小区。我想在此实现iOS 9的核心Spotlight功能,这样当用户打开应用程序时,它会从PFObjects索引数据。例如,应用程序(祈祷应用程序)中的用户说,他们需要祈祷成为酗酒者......其他人可以在手机上进入Spotlight Search,输入酒精类型,并显示具体的请求。我在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.") 
    } 
} 

回答

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

我想他们你想要的方式去了解这是使你加载完成后调用函数/呈现解析信息(即基本上随时随地你PFTableViewController的表视图更新)

你将不得不做一个for循环,并使其非常动态地获取每个单元格/行的信息。

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

至于打开的应用程序,也可以在你的appdelegate在您使用的NSUserActivity,通过它用户信息或不过想要去了解它,并从那里到信息使用的方法打开某个页面。

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

希望这有助于!让我知道如果我可以做任何其他事情来使这一点更清晰一点,因为我知道我不是最简洁的事情。快乐的编码!


回复@ user717452在这里评论为更好的造型

你提到SWIFT只让我表现出的外观目标C代码的Spotlight搜索等。

您可以循环遍历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"); }]; 
+0

我得到了如何创建它,更多的问题是关于如何循环访问PFObjects。 – user717452

+0

@ user717452更新了我的答案 – Chris