user717452
user717452

Reputation: 111

Core Spotlight in Parse Objects

My app uses Parse Objects that are put into a PFTableViewController. A user of the app can add in new content, and that will appear as a new cell for every one else using the app. I would like to implement iOS 9's Core Spotlight functionality in this, so that when a user opens the app, it indexes the data from the PFObjects. For example, a user in the app (prayer app) says they need prayers for being an alcoholic...someone else could go to Spotlight Search on phone, type in alcoholic, and that specific request would show up. The examples I have seen in the App Search Programming Guide for stuff like this are written in Swift, while I'm much more comfortable in Obj-C and would rather not re-write the entire class of this app to Swift just for this one feature. It also is for a single use item, could someone help guide me in converting to Obj-C and how to get it to index all 20 at a time that are visible, and continue to index when they page down?

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

Upvotes: 0

Views: 508

Answers (1)

Chris
Chris

Reputation: 1034

First off, no need to do it in Swift, there is Obj-C code and documentation.

Example:

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

I think they way you want to go about this is make a function that is called after you are done loading/presenting the Parse info (i.e. basically anytime your PFTableViewController's table view is updated)

You will have to do a for loop and make it very dynamic to take in each cell's/row's information.

As far as what is shown in the core spotlight search, you will have to have keywords for each item. Note that keywords are case sensitive, took me a while before I noticed. These keywords will be matched with that cell/row.

As far as opening the app, there is a method that can be used in you AppDelegate where you use the NSUserActivity, by it's user info or however you want to go about it, and get the information from there to open up a certain page.

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

Hope this helps! Let me know if I can do anything else to make this a bit more clearer since I know I'm not the best at making things concise. Happy coding!


Replying to @user717452 comment here for better styling

You mentioned swift only so I showed what objective c code for spotlight search looks like.

You can loop through your PFObjects, assuming it is an array of PFObjects, by just doing a regular for loop and a mutable array with a dictionary inside. Assuming your PFObject is like this: 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]}];
}

Then use the "mutArray" for 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"); }];

Upvotes: 1

Related Questions