IOS9 open new Api--spotlight use guide _ios

Source: Internet
Author: User

What's 1.Spotloight?

Spotlight has made some new improvements on iOS9, that is, opening up new APIs that you can integrate Spotlight in your app through the core Spotlight framework. An integrated Spotlight app can search for the contents of the app in spotlight and open related pages via content.

Demo Demo

  

2. How to Integrate Spotlight

A. Adding the required framework

Copy Code code as follows:

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

Note, many apps are supported by iOS9, so adding #if __iphone_os_version_max_allowed >= 90000 can solve the problem of running crashes for the following devices iOS9

B. Creating a Cssearchableitemattributeset object

Copy Code code as follows:

Cssearchableitemattributeset *attributeset = [[Cssearchableitemattributeset alloc] Initwithitemcontenttype: ( NSString *) Kuttypeimage];

Attributeset.title = Spotlighttitle; Title
attributeset.keywords = keywords; keywords, nsarray format
Attributeset.contentdescription = Spotlightdesc; Describe
Attributeset.thumbnaildata = photo; icon, NSData format

The way to convert a picture into a nsdata
Uiimagepngrepresentation ([UIImage imagenamed:@ "Xxx.png"]

C. Create a searchable entry Cssearchableitem

Copy Code code as follows:

Spotlightinfo can be used as a data transfer to the accepted place
Domainid ID, by this ID to determine which spotlight
Cssearchableitem *item = [[Cssearchableitem alloc] Initwithuniqueidentifier:spotlightinfo domainIdentifier:domainId Attributeset:attributeset];

D. Adding a search portal

Copy Code code as follows:

[[Cssearchableindex Defaultsearchableindex] Indexsearchableitems:@[item] completionhandler:^ (NSError * error) {
if (Error) {
NSLog (@ "Indexsearchableitems error:%@", error.localizeddescription);
}
}];

======== Complete Code ========

Copy Code code as follows:

-(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; Title
attributeset.keywords = keywords; keywords, nsarray format
Attributeset.contentdescription = Spotlightdesc; Describe
Attributeset.thumbnaildata = photo; icon, NSData format

Spotlightinfo can be used as a data transfer to the accepted place
Domainid ID, by this ID to determine which spotlight
Cssearchableitem *item = [[Cssearchableitem alloc] Initwithuniqueidentifier:spotlightinfo domainIdentifier:domainId Attributeset:attributeset];

[[Cssearchableindex Defaultsearchableindex] Indexsearchableitems:@[item] completionhandler:^ (NSError * error) {
if (Error) {
NSLog (@ "Indexsearchableitems error:%@", error.localizeddescription);

}
}];
}

======== How to load local pictures ========

Copy Code code as follows:
[Self insertsearchableitem:uiimagepngrepresentation ([uiimage imagenamed:@ "Xxx.png"]) spotlighttitle:@ "Wait for wind to come" Description:@ "The Wind to describe" keywords:@[@ "Bao", @ "Dahlia"] spotlightinfo:@ "pass the value of the past" domainid:@ "Com.wb.spotlight"];

How to use ======== to load network pictures ========

Copy Code code as follows:

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
NSData * data = [NSData datawithcontentsofurl:[nsurl urlwithstring:@ "http://hiphotos.baidu.com/doc/pic/item/ Eaf81a4c510fd9f905f61934262dd42a2934a48e.jpg "]];
[Self insertsearchableitem:data spotlighttitle:@ "wait for Wind to" description:@ "wind to describe" keywords:@[@ "Bao", @ "Dahlia"] Spotlightinfo: @ "Pass the past value" domainid:@ "Com.wb.spotlight"];
});

======== Delete all Spotlight methods ========

Copy Code code as follows:

[[Cssearchableindex Defaultsearchableindex] deleteallsearchableitemswithcompletionhandler:^ (NSError * _Nullable Error) {
if (Error) {
NSLog (@ "%@", error.localizeddescription);
}
}];

======== Delete the specified Spotlight method ========

Copy Code code as follows:

[[Cssearchableindex Defaultsearchableindex] deletesearchableitemswithdomainidentifiers:@ "DomainId" completionhandler:^ (Nserror * _nullable error) {
if (Error) {
NSLog (@ "%@", error.localizeddescription);
}
}];

======== the response method after clicking Spotlight ========

Copy Code code as follows:

-(BOOL) Application: (UIApplication *) application continueuseractivity: (nsuseractivity *) useractivity Restorationhandler: (void (^) (Nsarray * _nullable)) Restorationhandler {
if ([[[Useractivity Activitytype] isequaltostring:cssearchableitemactiontype]) {
NSString *uniqueidentifier = [Useractivity.userinfo objectforkey:cssearchableitemactivityidentifier];
Accept predefined values, and if multiple parameters can be passed with JSON to string, accept the string and convert it to JSON.
NSLog (@ "Pass over value%@", uniqueidentifier);
}
return YES;
}

Note:

Copy Code code as follows:

#if __iphone_os_version_max_allowed >= 90000
Related Spotlight methods, etc.
#endif

Spotlight supports more than iOS9 equipment to run, on with the lower version of the device need to join this to prevent crash problems

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.