Still cut to the chase, please point out any mistakes.

Spotlight has been around for a long time, and most people probably don’t think it’s useful. But if you have a lot of apps, like hundreds of apps, it’s hard to quickly find the apps you want on your desktop. That’s when Spotlight comes into play. IOS9 apple provides a more powerful app search function, previously only search to your app, from iOS9 you can search to a certain function in your app, users can directly enter the app through the search click a certain function module… Forget it. No ink. My writing style is very intoxicating, so let’s start with a simple use of Spotlight

First of all, I did some preparatory work, creating projects and so on,

As shown above, several simple interfaces are made for the subsequent jump ~~~ set several colors to distinguish which interface is used. Don’t ask me why I use these colors, because I guess you like yellow…

FirstView is orange view secondView is yellow view

Then import the header file # import < CoreSpotlight/CoreSpotlight. H > I am inside the appdelegate import, and at the time of application startup initialization and set up the search.

The method of set up Spotlight * create CSSearchableItemAttributeSet object

CSSearchableItemAttributeSet *firstSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"firstSet"];
Copy the code

// firstset. title = @” test firstView”; / / detailing firstSet contentDescription = @ “test firstView ha ha ha ha ha ha ha”; NSArray *firstSeachKey = [NSArray arrayWithObjects:@”first”,@” test “,@”firstView”, nil]; firstSet.contactKeywords = firstSeachKey; Note: The properties displayed in the search interface are set here. Show content, pictures, etc

* Create a CSSearchableItem object

CSSearchableItem *firstItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"firstItem" domainIdentifier:@"first" attributeSet:firstSet];
Copy the code

Note: Each set must correspond to an item.

* Set search

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:itemArray completionHandler:^(NSError * _Nullable error) {
Copy the code

If (error) {NSLog(@” failed to set %@”,error);

}else{NSLog(@” setup successful “); }}];

The search is set up like this. You can search for your content in Spotlight using the keywords you set. Here’s how

- (void)setSpotlight{
Copy the code

/ In-app search. Create as many sets as you want to search for. Each set corresponds to a item/CSSearchableItemAttributeSet * firstSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@”firstSet”]; // firstset. title = @” test firstView”; / / detailing firstSet contentDescription = @ “test firstView ha ha ha ha ha ha ha”; NSArray *firstSeachKey = [NSArray arrayWithObjects:@”first”,@” test “,@”firstView”, nil]; firstSet.contactKeywords = firstSeachKey; CSSearchableItemAttributeSet *secondSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@”secondSet”]; Secondset. title = @” Test SecondView”; SecondSet. ContentDescription = @ “test secondView ha ha ha ha ha ha ha ha”; NSArray *secondArrayKey = [NSArray arrayWithObjects:@”second”,@” test “,@”secondeVIew”, nil]; secondSet.contactKeywords = secondArrayKey; // when the user clicks on a search item, the system calls the proxy method and passes this UniqueIdentifier to you so that you can determine which one you clicked on. //domainIdentifier identifies the search domain. Delete entries called when the delegate will pass this value CSSearchableItem * firstItem = [[CSSearchableItem alloc] initWithUniqueIdentifier: @ “firstItem” domainIdentifier:@”first” attributeSet:firstSet]; CSSearchableItem *secondItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:@”secondItem” domainIdentifier:@”second” attributeSet:secondSet]; NSArray *itemArray = [NSArray arrayWithObjects:firstItem,secondItem, nil]; [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:itemArray completionHandler:^(NSError * _Nullable Error) {if (error) {NSLog(@” setup failed %@”,error);

}else{NSLog(@” setup successful “); }}]; }

After the – (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions launchOptions method call: (NSDictionary *)

NSLog(@"%f",[UIDevice currentDevice].systemVersion.floatValue);
Copy the code

If ([UIDevice currentDevice]. SystemVersion. FloatValue > = 9.0) {[self setSpotlight]; } Now set up the search and that’s it. But now when you click on it, you can only go to the app and not to the screen. You also need to implement the following proxy and do some operations in the proxy method

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
Copy the code

NSString *idetifier  = userActivity.userInfo[@”kCSSearchableItemActivityIdentifier”];     NSLog(@”%@”,idetifier);          UINavigationController *nav = (UINavigationController *)self.window.rootViewController;     UIStoryboard *stroy = [UIStoryboard storyboardWithName:@”Main” bundle:nil];          if ([idetifier isEqualToString:@”firstItem”]) {                  FirstViewController *fistVc = [stroy instantiateViewControllerWithIdentifier:@”firstView”];         [nav pushViewController:fistVc animated:YES];              }else if ([idetifier isEqualToString:@”secondItem”]){                  SecondViewController *secondVc = [stroy instantiateViewControllerWithIdentifier:@”secondView”];         [nav pushViewController:secondVc animated:YES];     }          return YES; }

~~~~ the system also provides several methods for deleting items

- (void)deleteSearchableItemsWithIdentifiers:(NSArray<NSString *> *)identifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler{ } - (void)deleteSearchableItemsWithDomainIdentifiers:(NSArray<NSString *> *)domainIdentifiers completionHandler:(void (^ __nullable)(NSError * __nullable error))completionHandler{ } - (void)deleteAllSearchableItemsWithCompletionHandler:(void  (^ __nullable)(NSError * __nullable error))completionHandler{ }Copy the code

Article Demo