IMPLEMENTING SPOTLIGHT IN YOUR APP
BhuShan PaWaR
Innovative Solution Architect specializing in Flutter, React Native, and iOS, driving the creation of exceptional cross-platform applications
Hey Readers, hope you have a great time reading the posts, today we will have a quick look at Spotlight search feature of iOS 9. Spotlight Search is new feature of iOS9 which enables us to provide content search of our Application to the Spotlight API.
By implementing spotlight user can search content in our without even opening our app. So are you ready for the Spotlight search drive ? Lets begin.
Step 1 :
Add following frameworks to
- CoreSpotlight Framework
- Mobile Core Service Framework
-
Step 2 :
Add following code to your view file.
-
- CSSearchableItemAttributeSet *attributeSet;
- attributeSet = [[CSSearchableItemAttributeSet alloc]
- initWithItemContentType:(NSString *)kUTTypeImage];
- attributeSet.title = @"Bhumesh";
- attributeSet.contentDescription = @"Super Spidy";
- attributeSet.keywords = @[@"bhumesh", @"spiderman",@"spidy"];
- UIImage *image = [UIImage imageNamed:@"spidy"];
- NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
- attributeSet.thumbnailData = imageData;
- CSSearchableItem *item = [[CSSearchableItem alloc]
- initWithUniqueIdentifier:@"com.aadil.SpotliteSearch"
- domainIdentifier:@"com.aadil.SpotliteSearch"
- attributeSet:attributeSet];
- [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item]
- completionHandler: ^(NSError * __nullable error) {
- if (!error)
- NSLog(@"Search item indexed");
- }];
- CSSearchableItemAttributeSet *attributeSet;
- attributeSet = [[CSSearchableItemAttributeSet alloc]
- initWithItemContentType:(NSString *)kUTTypeImage];
- attributeSet.title = @"Bhumesh";
- attributeSet.contentDescription = @"Super Spidy";
- attributeSet.keywords = @[@"bhumesh", @"spiderman",@"spidy"];
- UIImage *image = [UIImage imageNamed:@"spidy"];
- NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
- attributeSet.thumbnailData = imageData;
- CSSearchableItem *item = [[CSSearchableItem alloc]
- initWithUniqueIdentifier:@"com.aadil.SpotliteSearch"
- domainIdentifier:@"com.aadil.SpotliteSearch"
- attributeSet:attributeSet];
- [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item]
- completionHandler: ^(NSError * __nullable error) {
- if (!error)
- NSLog(@"Search item indexed");
Here make sure you supply your app bundle name “com.mybundle.SpotliteSearch” and proper keywords and you are good to go.
Run your project press control + shift + h and search you will see following search results except image as you need to add image to your results too.