Set title property from NSarray in CSSearchableItemAttributeSet
Asked Answered
C

2

1

I am trying to using CoreSpotlight API in application , I have plist file which has a several items on it for example animals' name . So I need to set title string equal to on of those object , for example if users search Lion , the line name and for example its features appears on the spotlight . Here is my code :

- (void)setupCoreSpotlightSearch
{

    CSSearchableItemAttributeSet *attibuteSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(__bridge NSString *)kUTTypeImage];


    NSURL *url = [[NSBundle mainBundle] URLForResource:@"animals" withExtension:@"plist"];
    NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];
    NSString *getNames = [NSString stringWithFormat:@"%@",playDictionariesArray];
    NSLog(@"%@",getNames) ;


    attibuteSet.title =getNames;
    attibuteSet.contentDescription = @"blah blah ";


    CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"app name"
                                                               domainIdentifier:@"com.compont.appname"
                                                                   attributeSet:attibuteSet];
    if (item) {
        [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"Search item indexed");
            }
        }];
    }
}

The problem is getNames returns all names !!! how can I filter it when is user is searching an specific word from animals.plist Thanks .

EDIT [Plist Image]: enter image description here

Chock answered 30/10, 2015 at 19:48 Comment(0)
S
1

You can maintain NSArray and iterate through playDictionariesArray, creating & initialising CSSearchableItem object with that particular entry in your data source.

- (void)setupCoreSpotlightSearch
{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"animals" withExtension:@"plist"];
    NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];
    NSMutableArray * searchableItems = [[NSMutableArray alloc]init];
    for(object in playDictionariesArray)
    {
        CSSearchableItemAttributeSet *attibuteSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(__bridge NSString *)kUTTypeImage];

        attibuteSet.title =[NSString stringWithFormat:@"%@",object]; //retrive title from object and add here
        //attibuteSet.contentDescription = @"blah blah "; // retrieve description from object and add here


        CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"app name"
                                                           domainIdentifier:@"com.compont.appname"
                                                               attributeSet:attibuteSet];
        [searchableItems addObject:item];
    }

   if (searchableItems) {
       [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
            if (!error) {
               NSLog(@"Search item indexed");
            }
       }];  
   }
}

I haven't ran and tested the code.

Shwa answered 30/10, 2015 at 21:27 Comment(7)
Why did you remove attibuteSet.title ?? This is my main issue and must have a valueChock
playDictionariesArray will contain list of all the names. So you can set title as attributeSet.title = object. You can check the edit.Shwa
I change object to for(NSString *name in playDictionariesArray) and set attributeSet.title = name but the problem is only last item of my plist file is searchable which is Birds I have many other animals !Chock
Then you need iterate through to only the last object i.e. Birds. for(NSString *name in [playDictionariesArray objectAtIndex:[playDictionariesArray count]-1])Shwa
Thanks ! but app crashes now ! Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instanceChock
Can you please share structure of your plist file?Shwa
Let us continue this discussion in chat.Shwa
J
0

you are not looping through each key. Use the code provided in this question.

CoreSpotlight indexing

Try it on a device that supports indexing. NOT iPhone 4/4s or iPad.

Jocose answered 3/11, 2015 at 15:23 Comment(2)
You code doesn't call NSLog(@"Search item indexed"); at all ! it seems nothing work . I call it self spotLightIndexing] in viewDidLoadChock
Call it on viewWillAppear... But what you should do is set a bool to run only once. Otherwise it will run on every launch.Jocose

© 2022 - 2024 — McMap. All rights reserved.