IOS9 Spotlight and ios9spotlight

Source: Internet
Author: User
Tags convert json to string convert string to json string to json

IOS9 Spotlight and ios9spotlight

1. What is Spotloight?

Spotlight has made some new improvements on iOS9, that is, opening up some new APIs. With the Core Spotlight Framework, You can integrate Spotlight in your app. Apps integrated with Spotlight can search for App content in Spotlight and open relevant pages through the content.

Demo

  

 

2. How to Integrate Spotlight

A. Add the required framework

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000#import <CoreSpotlight/CoreSpotlight.h>#import <MobileCoreServices/MobileCoreServices.h>#endif

Note: Many apps support less than iOS9, So adding # if _ IPHONE_ OS _VERSION_MAX_ALLOWED> = 90000 can solve the problem of running crashes on devices lower than iOS9.

B. Create a CSSearchableItemAttributeSet object

CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType :( NSString *) kUTTypeImage]; attributeSet. title = spotlightTitle; // The title attributeSet. keywords = keywords; // keyword, attributeSet in NSArray format. contentDescription = spotlightDesc; // describes attributeSet. thumbnailData = photo; // icon in NSData format

// Convert an image to NSData
UIImagePNGRepresentation ([UIImage imageNamed: @ "xxx.png"]

C. Create a searchable CSSearchableItem

// SpotlightInfo can be passed as some data to the accepted location // domainId. This id is used to determine which spotlightCSSearchableItem * item = [[CSSearchableItem alloc] initWithUniqueIdentifier: spotlightInfo domainIdentifier: domainId attributeSet: attributeSet];

D. Add a search entry

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * error) {     if (error) {        NSLog(@"indexSearchableItems Error:%@",error.localizedDescription);     } }];

 

======= Complete code ========

-(Void) insertSearchableItem :( NSData *) photo spotlightTitle :( NSString *) spotlightTitle description :( NSString *) spotlightDesc keywords :( NSArray *) keywords spotlightInfo :( NSString *) spotlightInfo domainId :( NSString *) domainId {CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType :( NSString *) kUTTypeImage]; attributeSet. title = spotlightTitle; // The title attributeSet. keywords = keywords; // keyword, attributeSet in NSArray format. contentDescription = spotlightDesc; // describes attributeSet. thumbnailData = photo; // icon, NSData format // spotlightInfo can be used as some data to be passed to the accepted location // domainId, this id is used to determine which spotlight CSSearchableItem * item = [[CSSearchableItem alloc] region: spotlightInfo domainIdentifier: domainId attributeSet: attributeSet]; [[CSSearchableIndex defasearchsearchableindex] indexSearchableItems: @ [item] completionHandler: ^ (NSError * error) {if (error) {NSLog (@ "indexSearchableItems Error: % @", error. localizedDescription) ;}}] ;}

 

======== How to load a local image ========

 

[Self insertSearchableItem: UIImagePNGRepresentation ([UIImage imageNamed: @ "xxx.png"]) spotlightTitle: @ "" description: @ "" keywords: @ [@ "Bao whale", @ "da Li Hua"] spotlightInfo: @ "Pass past value" domainId: @ "com. wb. spotlight "];

 

========= How to use network image loading ========

 

Dispatch_async (events, 0), ^ {NSData * data = [NSData dataWithContentsOfURL: [NSURL URLWithString: @ "http://hiphotos.baidu.com/doc/pic/item/eaf81a4c510fd9f905f61934262dd42a2934a48e.jpg"]; [self insertSearchableItem: data spotlightTitle: @ "" description: @ "" keywords: @ [@ "Bao whale", @ ""] spotlightInfo: @ "Pass past value" domainId: @ "com. wb. spotlight "] ;});

 

======== Method for deleting all spotlight ==========

 

[[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {     if (error) {         NSLog(@"%@", error.localizedDescription);     } }];

 

======== Method for deleting the specified spotlight ========

 

[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:@"domainId" completionHandler:^(NSError * _Nullable error) {     if (error) {         NSLog(@"%@", error.localizedDescription);     } }];

 

======= Response method after you click spotlight ==========

 

-(BOOL) application :( UIApplication *) application continueUserActivity :( NSUserActivity *) userActivity restorationHandler :( void (^) (NSArray * _ Nullable) restorationHandler {if ([userActivity activityType] isw.tostring: CSSearchableItemActionType]) {NSString * uniqueIdentifier = [userActivity. userInfo objectForKey: CSSearchableItemActivityIdentifier]; // accept predefined values. If multiple parameters are used, convert json to string and pass them, convert string to json NSLog (@ "passed Value % @", uniqueIdentifier);} return YES ;}

 

Note:

# If _ IPHONE_ OS _VERSION_MAX_ALLOWED >=90000
// Spotlight Methods
# Endif

// Spotlight supports running on iOS9 and later devices. You need to add this feature to devices of earlier versions to prevent crash.

 

If you did not see this article in wb145230 blog,Click to view Original Text.

 

 

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.