Can apps provide a hook for Spotlight to search content within the app?
Asked Answered
F

4

8

Example: App contains messages. User searches spotlight with string from message. Spotlight finds that app.

I have heard that spotlight can search app contents. But how to feed it to Spotlight on iOS?

Flyblown answered 30/7, 2013 at 13:9 Comment(4)
I guess you heard wrong, I can not find a single thing in the documentation about in integration with spotlight on iOS.Lillis
developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…Dehart
@openfrog, i have not seen it done in iphone and ipad but it can be done with mac, here is the link to spotlighter sample code for mac if that could help. developer.apple.com/library/mac/#samplecode/Spotlighter/…Alsace
This has changed with the updates to iOS9. There are now Search API's to add app content to Spotlight. Please see my answer belowStandoff
B
7

According to the Core Data Spotlight Integration Programming Guide, the functionality you want is not available for iOS, only for Mac OS X.

Basir answered 30/7, 2013 at 14:11 Comment(1)
This has changed with the updates to iOS9. There are now Search API's to add app content to Spotlight. Please see my answer below.Standoff
P
5

This is now possible from iOS9 onwards.

Apple has released a CoreSpotlight SDK (WWDC2015) where, you can integrate your app to the spotlight of iOS and can do a content search.

There are other possible avenues to actually integrate different user activities to your app and can also search for things even when your app is not installed on the device.

If your app is an app that handles pdf for instance, if user searches for a pdf on his device, you app can come up in the spotlight preferences as an app he can use to read the pdf, even when your app is not installed on the user's device.

Considering your example, now its possible that if you search for a message string in spotlight, spotlight can open up your app and you can make the user actually navigate to find the exact message inside your app as well.

Adding link below : You can find details for implementation.

https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS9.html#//apple_ref/doc/uid/TP40016198-SW3

-Tejas

Proudfoot answered 11/6, 2015 at 9:36 Comment(3)
which means for all the possible search terms related to our app, need to be added in CSSearchableIndex as SearchableItem?Trajectory
Short Answer : Yes. Long Answer: Search actually gives you finer control of what you would need to search in your app's contents. So you would have to index every object you would need to search from spotlight by adding a CSSearchableItem.Proudfoot
Added a link in my answer. You can get more details about the terms.Proudfoot
S
1

Here is an example of adding your app content to Spotlight via the new Search API's. This is available on iOS9 using XCode 7.

CSSearchableItemAttributeSet * attributes = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage]; //Or whatever type
attributes.contentDescription = @"This is my content description.";
attributes.displayName = @"Display Name";
attributes.keywords = @["Stuff","Widget"];
attributes.subject = @"Subject";
attributes.title = @"Title";

CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:someUniqueId domainIdentifier:@"SomeGroupName" attributeSet:attributes];

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:nil];

When the user selects the item in spotlight, the following method:

-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler

in your AppDelegate will be called. Check the userInfo dictionary in the userActivity object and send the user to the appropriate screen.

Standoff answered 17/6, 2015 at 20:17 Comment(2)
So how the search is working here..user should search the title or for the keywords?Trajectory
Any one of those would work. The title, keywords,subject everything becomes a keyword.Proudfoot
U
1

I have created a sample project to integrate corespotlgiht feature. It works on iOS 9 and requires Xcode 7 beta 2 to build. You can try out if it helps. https://github.com/majain/iPhoneCoreDataRecipes

Video link for the same is: https://youtu.be/Renm1xLDIFc

Unmixed answered 27/6, 2015 at 23:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.