How to get text entered in spotlight search?

146 Views Asked by At

I have implemented spotlight searching in my app. It is working fine with static text. I want to get the text that is entered in spotlight search bar so that i can display the text according to it.

-(void)setUpCoreSpotlight
{

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

attributeSet.displayName = [NSString stringWithFormat:@"%@ - Currently playing with student %@",kApplicationName,[[ModelDataManager sharedModelManager] objSelectedStudent].StudentName];
attributeSet.title = kApplicationName;
attributeSet.contentDescription = [NSString stringWithFormat:@"Curriculums assigned to %@ : %@",[[ModelDataManager sharedModelManager] objSelectedStudent].StudentName, [[ModelDataManager sharedModelManager] objSelectedStudent].CurriculumName];

NSMutableArray *arrStudents = [[[ModelDataManager sharedModelManager]arrStudents] valueForKey:@"StudentName"];
NSMutableArray *arrKeywords = [[NSMutableArray alloc]initWithObjects:@"student",@"colorskit",@"play",@"prrogram", nil];
[arrKeywords addObjectsFromArray:arrStudents];

attributeSet.keywords = arrKeywords;
UIImage *image = [UIImage imageNamed:@"CC-Cover"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attributeSet.thumbnailData = imageData;


CSSearchableItem *item1 = [[CSSearchableItem alloc]
                           initWithUniqueIdentifier:@"program"
                           domainIdentifier:@"http://www.colorsacademy.com/in/"
                           attributeSet:attributeSet];

attributeSet = [[CSSearchableItemAttributeSet alloc]
                initWithItemContentType:(NSString *)kUTTypeItem];    
attributeSet.displayName = kApplicationName;
attributeSet.title = kApplicationName;
attributeSet.contentDescription = @"You can teach this program ";
attributeSet.keywords = keywords;
image = [UIImage imageNamed:@"enzymes-cover"];
imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attributeSet.thumbnailData = imageData;
...
...
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)activity restorationHandler:(void (^)(NSArray *))restorationHandler
{

NSString * valueCSSearchableItemActionType;
BOOL wasHandled = NO;

if ([CSSearchableItemAttributeSet class]) //iOS 9
{

    valueCSSearchableItemActionType = CSSearchableItemActionType;

} else { // iOS 8 or earlier

    valueCSSearchableItemActionType = @"not supported";
}

if ([activity.activityType isEqual: valueCSSearchableItemActionType])
    //Clicked on spotlight search, item was created via CoreSpotlight API
{

    //…handle the click here, we can assume iOS 9 from now on…
    NSString * activityIdentifier = [activity.userInfo valueForKey:CSSearchableItemActivityIdentifier];
    wasHandled = YES;
    NSLog(@"Continuing user activity %@", activityIdentifier);

} else {

    //the app was launched via Handoff protocol
    //or with a Universal Link
}

return wasHandled;
}

I am not able to get the text has been entered. Even "application continueUserActivity" method also not being called. Please help if anybody have idea about it.

0

There are 0 best solutions below