Search APIs in Spotlight and Safari
-
Upload
yusuke-kita -
Category
Engineering
-
view
3.690 -
download
4
Transcript of Search APIs in Spotlight and Safari
Popular public results available to other users
• Multiple users engage with device results• Results in Search and Safari (if URL included)
NSUserActivity and App Search• Activities can be designated as searchable
• Activities can be designated as public
• Add indexable metadata
• Results in iOS Search and Safari
• Revisit activity on Search selection
NSUserActivity APIEnable Capabilitiesvar eligibleForHandoff: Boolvar eligibleForSearch: Boolvar eligibleForPublicIndexing: Bool
NSUserActivity APIProvide Attributes and Keywordsvar title: String?var keywords: Set<String>var contentAttributeSet: CSSearchableItemAttributeSet?var expirationDate: NSDate
Create Activityvar activity: NSUserActivity = NSUserActivity(activityType: "com.example")
activity.title = "Baked Potato Chips"activity.userInfo = ["id": "http://www.example.com/recipe/111"]
activity.eligibleForSearch = trueactivity.eligibleForPublicIndexing = trueactivity.becomeCurrent()
Continue ActivityUIApplicationDelegatefunc application(UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: [AnyObject]? -> Void) -> Bool { if userActivity.activityType == "com.example" { // Restore state for userActivity and userInfo }
return true}
CoreSpotlight
• For any app content
• Methods to add, update and delete items
• Used by Messages, Mail, Calendar and Notes
Create Item
// Create attributeSet and populate with metadatalet attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTypeImage as String)
attributeSet.title = "Haleakala Sunrise"attributeSet.contentDescription = "May 12, 2015 Maui, Hawaii"
// Create item with unique identifier, domainIdentifier used to group itemslet item = CSSearchableItem(UniqueIdentifier: "1", domainIdentifier: "album-1", attributeSet: attributeSet)
// Index itemCSSearchableIndex.defaultSearchableIndex().indexSearchableItems([item]) { error in if error != nil { print(error?.localizedDescription) } else { print("Item indexed!") }}
Restore
// Application delegate called when CoreSpotlight result is tappedfunc application(UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: [AnyObject]? -> Void) -> Bool { if userActivity.activityType == CSSearchableItemActionType { let UniqueIdentifier = userActivity.userInfo? [CSSearchableItemActivityIdentifier] as? String // Use 'UniqueIdentifier' } }
Update
Same method as adding item to indexfunc indexSearchableItems(items: [CSSearchableItem], completionHandler: ((NSError?) -> Void)?)
Delete
Identifiersfunc deleteSearchableItemsWithIdentifiers(identifiers: [String], completionHandler: ((NSError?) -> Void)?)
identifiers = ["1", "4", "5"]
Delete
DomainIdentifierfunc deleteSearchableItemsWithDomainIdentifiers(DomainIdentifiers: [String], completionHandler: ((NSError?) -> Void)?)
domainIdentifiers = ["domain-2"]
Delete
Allfunc deleteAllSearchableItemsWithCompletionHandler(completionHandler: ((NSError?) -> Void)?)
Indexing Web Content
• The content driving your app may live on the web, not locally inside the app
• Deep linked pages from your app's website are indexed for app search
Enabling app search
1. Allow Apple to discover and index your app's website
2. Ensure your app's website contains markup for mobile deep links
3. Enable deep link handling for your app
4. Add markup for structured data (optional, but highly recommended)
Smart App Banners
if app is not installed<meta name="myApp" content="app-id=123, app-argument=http://example.com/about>
Deep Linking• Universal link
https://developer.apple.com/videos/wwdc/2014/?include=101#101
• Custom URL schemeapple://wwdc/2014/?include=101#101
Support Universal Link in your server
• Create your "apple-app-site-association" file
• Generate an SSL certification
• Sign your file
• Upload to your server
Support Universal Link in your app
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]!) -> Void) -> Bool
var webpageURL: NSURL?var activityType: Stringlet NSUserActivityTypeBrowsingWeb: Stringclass NSURLComponents: NSObject
Support Custom URL scheme
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { // In this example, the URL is http://example.com/profile/?123 if let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: true), let path = components.path, let query = components.query { if path == "/profile" { // Pass the profile ID from the URL, to the view controller return profileViewController.loadProfile(query) } } } return false}
Supported schemas
• AggregateRating
• Offers
• PriceRange
• InteractionCount
• Organization
• Recipe
• SearchAction
Better satisfaction and engagement
• Thumbnail, well-structured description, ratings and actions
• Relevant and appealing image
• Key information user is looking for
Keywords association
• Typical items should have 3 to 5 keywords
• Category keywords, e.g. "ticket" or "recipe"
• Synonyms or abbreviations for item subject
UI Behavior on App Launch
• Go straight to content, no interstitials or multi-step builds
• Guidelines apply to both app and web links
• Time from result to content is measured and used in ranking
Known Issues• Tapping a Mail message, Maps favorites, Note, or Message
in search results does not open the app on some devices
• When submitting an activity to be searchable with NSUserActivity the metadata (attrituteSet) does not get added to the index
See iOS9 Release Notes for more detail