New Features of iOS9-Search APIs

Source: Internet
Author: User

New Features of iOS9-Search APIs

Before IOS9, you can only use spotlight to find the corresponding app by the app name. However, with the release of iOS9 Search APIs, developers can index the content in the app and enter the content area specified by the app by searching for keywords.

The 3 APIs

NSUserActivity

The NSUserActivity is described in The Handoff usage of iOS8. In iOS9, you can use NSUserActivity to search for "active information ". You can provide some keywords for these "active information", which means spotlight can retrieve them. This operation is equivalent to having a history record when you browse a Web page. You can use spotlight to quickly open the latest "active information ".

Web Markup

Web Markup allows apps to map their content to the Web page, and then facilitates spotlight to quickly retrieve content on the Web page. Apple's indexer will assume the role of crawler to search for the content that has been Markup on the webpage. This information can be searched in both Safari and Spotlight.

Core Spotlight

Core Spotlight is a new framework in iOS9 that allows you to retrieve content in the app. NSUserActivity is useful in saving user history, while Core Spotlight can retrieve any data you want.

Use Core Spotlight APIs

NSUserActivity and Web Markup APIs are relatively easy to use, while Core Spotlight is a little complicated. To demonstrate how Core Spotlight works, we create a simple Demo to show the list of friends. Then you can click the name of any friend to view the specific information of the friend's profile picture. Before demonstrating the specific process, let's take a look at the final process.

In the demo, we can see that I searched for related friend information in spotlight to see their rough information, and then clicked a piece of information to jump to the specific friend details in my app.

 

Code Analysis:

1. the Demo structure is very simple. It is a navigation controller, and the root controller is a UITableViewController, which is used to display the list of friend names. We define it as FriendTableViewController.

2. Click a friend name in the list to go to the details page. We define itFriendViewController。

3. We use a management class to manage all the Friends' data information. We define it as DataSource. Responsibilities of this management class:

1) store all friend data information.

2) Save the user information to the Core Spotlight indexer.

Of course, each piece of user information corresponds to a model, which is defined as follows:

Person. h

 

@interface Person : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *identifer;@property (nonatomic, copy) NSString *icon;- (instancetype)initWithName:(NSString *)name identifer:(NSString *)identifer icon:(NSString *)icon;@end
Person. m

 

 

@implementation Person- (instancetype)initWithName:(NSString *)name identifer:(NSString *)identifer icon:(NSString *)icon {    if (self = [super init]) {        self.name = name;        self.identifer = identifer;        self.icon = icon;    }    return self;}@end

 

The DataSource. h method list is as follows:

 

@interface Datasource : NSObject- (NSArray *)dataList;- (Person *)findFriendWithId:(NSString *)identifer;- (void)savePeopleToIndex;@end

1) The dataList method is used to obtain the data information of all user lists.

 

2) findFriendWithId: The method is to obtain model data based on the user ID.

3) savePeopleToIndex stores all user data information in the Core Spotlight indexer.

Code in DataSource. m file

 

@implementation Datasource- (NSArray *)dataList {    Person *becky = [[Person alloc] initWithName:@Becky identifer:@1 icon:@becky];        Person *ben = [[Person alloc] initWithName:@Ben identifer:@2 icon:@ben];    Person *jane = [[Person alloc] initWithName:@Jane identifer:@3 icon:@jane];    Person *pete = [[Person alloc] initWithName:@Pete identifer:@4 icon:@pete];    Person *ray = [[Person alloc] initWithName:@Ray identifer:@5 icon:@ray];    Person *tom = [[Person alloc] initWithName:@Tom identifer:@6 icon:@tom];        return @[becky, ben, jane, pete, ray, tom];}- (Person *)findFriendWithId:(NSString *)identifer {    for (Person *p in self.dataList) {        if ([p.identifer isEqualToString:identifer]) {            return p;        }    }    return nil;}- (void)savePeopleToIndex {    // prepare    NSMutableArray *searchableItems = [NSMutableArray array];    for (Person *p in self.dataList) {        // Create an attribute set for an item that represents an image.        CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@image];        attributeSet.title = p.name;        attributeSet.contentDescription = [NSString stringWithFormat:@This is an entry all about the interesting person called %@, p.name];        attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:p.icon]);                CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:p.identifer domainIdentifier:@com.ios9daybyday.SearchAPIs.people attributeSet:attributeSet];        [searchableItems addObject:item];    }        // save    [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {        if (error) {            NSLog(@error message:%@, error.localizedDescription);        }    }];}@end
The key part of the code is the savePeopleToIndex method. The defined searchableItems is used to store the relevant searchable information; in the code, the single-sample method of CSSearchableIndex indexSearchableItems is a real operation to store the content in searchableItems to Core Spotlight.

 

Then let's take a look at the main code on the page displayed in the FriendTableViewController list.

 

- (void)viewDidLoad {    [super viewDidLoad];    Datasource *dataSource = [Datasource alloc];    self.dataList = [dataSource dataList];        [dataSource savePeopleToIndex];}
Here, the savePeopleIndex stores the content in Core Spotlight.

 

Now that you run the program, the data will be stored. When you search for your friends in spotlight, they will appear as follows:

Click an item, but it will not jump to the specified area of the app. It will only jump to the corresponding app, because we have not specified the area to jump.

We can specify the app behavior when calling the continueUserActivity proxy method. The Code is as follows:

 

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler NS_AVAILABLE_IOS(8_0) {        NSString *friendID = userActivity.userInfo[@kCSSearchableItemActivityIdentifier];    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;    [navigationController popToRootViewControllerAnimated:NO];        FriendTableViewController *friendVC = (FriendTableViewController *)navigationController.viewControllers.firstObject;    [friendVC showFriendWithId:friendID];        return YES;}

Code 1st: Get the id of the friend you clicked in spotlight.

 

Code 2nd and 3: Get the root navigation control and pop all controllers in the stack.

Code 4th: Jump to the specified position of the app.

The code here shows that the content we saved to the Core SpotLight indexer can now be obtained using the userActivity. userInfo dictionary. What we care about is the friend Id, which is the kCSSearchableItemActivityIdentifier stored in the indexer as the object of Person.

As you can see, there is a Back to search option in the upper left corner. You can click here to return to the user list.

In this article, the deletion operation of the indexer is not involved. To learn about the specific operation of the deletion, refer to the following methods:

 

deleteSearchableItemsWithIdentifiersdeleteSearchableItemsWithDomainIdentifiersdeleteAllSearchableItemsWithCompletionHandler

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.