iOS9 Search API 詳解

SearchAPI是什么

iOS 9.0 系統以后,下拉屏幕或者向右滑到最后會顯示"spotlight"頁面(不支持以下可以升級到iOS9的設備:iPhone 4S,iPad 2,iPad第三代,iPad mini, iPod touch第5代)

spotlight.png

在搜索欄中搜索關鍵詞,會顯示不同app的內容,如圖一

圖一.png

點擊『豆瓣』一欄里的第一行,直接跳入了『豆瓣』app,顯示了『荒野獵人』的頁面

圖二.png

這就是SearchAPI的一個用法。

官方文檔App Search Programming Guide中的介紹是

Search in iOS 9 gives people great ways to access information inside of your app, even when it isn’t installed. When you make your content searchable, users can access activities and content deep within your app through Spotlight and Safari search results, Handoff, Siri Suggestions, and Reminders. Making your content searchable helps you enhance the user experience of your app and improve its discoverability.

iOS 9 的搜索功能為人們獲取app中的信息提供了極大的便利,即便是在該app并未安裝的情況下。 當你的app中的內容標記為可搜索時,用戶可以通過Spotlight,Safari,Handoff,Siri Suggestion ,Reminders來獲取到這些頁面和內容。讓你的app的內容可以被搜索到,這能夠幫助用戶發現你的app,并且提升用戶體驗。

App search is easy for you to adopt and customize. You don’t need any prior experience with implementing search, and you control what content gets indexed, which information to show in search results, and where the user goes after tapping a result related to your content.

App search 功能很容易使用和定制。你自己控制哪些內容是被標記為可搜索的,哪些信息是展示在搜索結果中(spotlight,safari等),用戶點擊了你的搜索結果之后頁面跳去哪里,并不需要在此之前有過implementing search的經驗。

iOS 9 中隱私性是一個基本的特征。為了在保護用戶隱私數據的情況下給用戶最好的搜索體驗,這里有兩種索引(index)方式來存儲可被搜索的內容。你可以使用各種搜索相關APIs來對每個item指定合適的索引(index),因此,明白每種索引(index)是如何工作的就至關重要。

iOS 9 provides the following indexes:
A private on-device index. Each device contains a private index whose information is never shared with Apple or synced between devices. When you make an item available in a user’s on-device index, only that user can view the item in search results.
Apple’s server-side index. The server-side index stores only publicly available data that you’ve marked appropriately on your website

iOS 9 提供了兩種索引方式:
  • 私有的設備側索引。每個設備包含私有的索引,被其索引的信息永遠不會跟Apple共享或者在設備之間同步。當一個條目被標記為用戶側索引,只有該設備的用戶才能在搜索結果中看到該類目。
  • Apple 服務器側索引。服務器側索引只存儲,你的網站上的,已經恰當的標記為公開可見的數據。
iOS 9 提供了一些APIs:
  • NSUserActivity類中新增了方法和屬性。這些可以幫助你索引用戶在app的活動,比如visiting a navigtion point ,創建和查看內容。
  • Core Spotlight framework。它提供的APIs能把app特定內容添加到用戶側索引,并且開啟跳入到你的app的deep links。(所謂deep link 指的是點擊spotlight或者safari的搜索結果,跳進你的app中的指定頁面)
  • Web markup。這個能把相關的web 內容可搜索化,幫助你豐富用戶的搜索體驗。

如何選擇合適的APIs?

官方文檔給出了下面的信息。

It’s recommended that all apps use NSUserActivity and that they support Handoff. Using the search-related properties of NSUserActivity is the best way to show users the information they care about and to improve the ranking of your search results. As users perform activities in your app, you use NSUserActivity to add the item to the on-device index. By default, an item represented by an NSUserActivity object is private, which means that it is added to the on-device index. If your content includes activities that all users can view, you identify them as eligible for public indexing. Learn more about using NSUserActivity in Index Activities and Navigation Points.

If your app handles persistent user data, such as documents, photos, and other types of content created by or on behalf of users, use the Core Spotlight APIs to index the content. Unlike NSUserActivity, Core Spotlight does not require users to visit the content in order to index it. In addition, you can use CoreSpotlight APIs to index content at any point, such as when the app loads. Note that Core Spotlight helps you make items searchable in the private on-device index; you don’t use Core Spotlight APIs to make items publicly searchable. Learn more about using Core Spotlight APIs in Index App Content.

If your app hosts some or all of its content on a website, use web markup to let Apple’s web crawler (calledApplebot) index your content in Apple’s server-side index and make it available to all iOS users in Spotlight and Safari search results. Learn more about using web markup to make your web content searchable in Mark Up Web Content. Note that search results generated by web markup are shown in countries that are supported by Spotlight Suggestions.

蘋果推薦所有app 都使用NSUserActivity 來提供Handoff功能。使用NSUserActivity中搜索相關的屬性,是展示用戶關心的信息,提升在搜索結果中的排名的最好的方式。用戶在你的app中操作的時候,你使用NSUserActivity把當前活動增加到用戶側索引中。默認情況下,使用NSUserActivity抽象的條目是私有的,這意味著它被存儲在用戶側索引中。如果你的內容中包括所有用戶都可見的活動,你應該把這些內容聲明為公開。點擊Index Activities and Navigation Points查看更過關于如何使用NSUserActivity的信息。

如果你的app操作了持久化的信息,比如文檔,圖片,其他類型的(被用戶或者為了用戶創建的)內容,那你應該使用Core Spotlight APIs來索引它們。不同于NSUserActivity,Core Spotlight并不要求先讓用戶訪問內容才能索引這些。另外,你可以使用CoreSpotlight APIs在任何節點索引數據,比如在app加載的時候。值得注意的是,使用Core Spotlight標記的數據都是用戶側的,你并不能把item標記為公開可搜索的。點擊Index App Content查看更多關于使用Core Spotlight APIs的信息。

如果你的app在網站上托管部分或者全部數據,使用web markup 可以讓Apple的網頁爬蟲(叫做Applebot)把內容索引到Apple服務器側,這使得這些內容在所有iOS 用戶的Spotlight 和Safari搜索結果中都是可見的。請注意:使用web markup生成的搜索結果會展示在Spotlight Suggestions支持的國家。

使用NSUserActivity的索引聲明為公開內容有什么作用?我在官方文檔里找到這樣一段話

In iOS 9, marking a public item as eligible for public indexing also adds it to the on-device index and confers an additional advantage: When you use web markup to make your related website content searchable, user engagement with publicly eligible search results from your app can help improve the ranking of your website’s content. When a user taps a searchable activity or state in Spotlight search results, you use NSUserActivity APIs to continue the activity and return the user to the relevant area in your app.

在iOS 9中,把一條公共類目標記為eligibleForPublicIndexing也是存儲到on-device index,這給予了更多的功能:當你使用web markup技術把你的相關網頁內容可搜索化時,你的app的公開搜索結果的用戶參與度,可以幫助你提升你的網頁內容的排名。當用戶點擊了spotlight 搜索結果中的活動或者狀態時,使用NSUserActivity APIs 幫助用戶繼續之前的活動,返回到app中相關頁面。
綜合上文,使用NSUserActivity把內容標記為公開,可以讓安裝了app的其它用戶搜索到相關信息。舉個例子,淘寶電影app。A用戶訪問了『美人魚』電影頁面,這個時候app內部把該(由美人魚電影相關信息封裝而成)類目標記為公開;然后B用戶也執行此操作;C用戶,D用戶...。當產生了許多個(未知,沒公布)相同標記之后(每個用戶訪問美人魚頁面在其設備上生成一個標記),沒有訪問過該頁面的X用戶Y用戶Z用戶在spotlight中搜索『美人魚』,也會出現該搜索結果(需要用戶安裝淘寶電影app)。

隱私

這三種APIs ,跟上文說的兩種索引方式是對應的。使用NSUserActivity和Core Spotlight APIs創建的索引都是 on-device index.
使用web markup技術是存儲在server-side

As the user uses your app, you create activity objects associated with various navigation points and app states. Each item is added to the on-device index by default. In iOS 9, marking a public item as eligible for public indexing also adds it to the on-device index.

The Core Spotlight framework provides ways to index the content within your app and manage the private on-device index.The items you index using Core Spotlight APIs are not added to Apple’s server-side index or synced between devices.

Because items indexed by Applebot are already public, they are stored on Apple’s server-side index.

輔助功能

除了使用NSUserActivity 和 Core Spotlight的APIs,增加web markup之外,你還應該使用三個關鍵技術來給用戶最好的體驗。

  • Univeral links。 在iOS 9 之后,使用universal links技術,用standard HTTP or HTTPS links取代custom URL schemes。Universal links對所有用戶有有效:如果用戶安裝了你的app,link會把他們引入你的app;如果用戶沒有安裝你的app,lin 會在Safari打開你的網站。點擊Support Universal Links了解如何使用universal links。

  • Smart App Banners。當用戶在Safari中訪問你的網頁,Smart App Banner 可以讓用戶啟動你的app(如果有安裝)或者有機會下載你的app(如果未安裝)。點擊Promoting Apps with Smart App Banners了解更多關于Smart App Banners 的信息。

  • Handoff。Handoff 允許用戶從一個設備切換到另一個設備時繼續之前的活動。例如,當在Mac瀏覽一個網頁時,在你的iPad上,會直接進入你的原生app中。在iOS 9 中,Handoff包括了對app search的特殊支持。點擊Handoff Programming Guide了解更多關于Handoff 的信息。

關于搜索結果的排名

為了給用戶最好的體驗,系統會估計用戶使用Spotlight或者Safari搜索跳入app的頻次。正因為如此,那些用戶覺得沒什么用的搜索結果會很快的被標記出來,最終不會再搜索結果中展示。
iOS 會根據下面的信息來決定搜索結果展示的順序:

  • 用戶通過搜索訪問app內容的頻次(only NSUserActivity)
  • 用戶通過搜索訪問app的數量
  • 使用web markup標記的網頁的訪問量和有效結構化的數據量。

總得來說,你可以通過以下的事情來提供良好的搜索結果以此鼓勵用戶使用你的app:

  • app內提供優質內容
  • 使用上文提到的搜索APIs來提升搜索結果與你的app的相關姓
  • 盡可能的只把用戶最喜歡的數據供搜索使用
  • 必要的情況下通過移除和更新來保持索引的時效性
  • 提供切實豐富的搜索信息來引導用戶點擊(搜索結果)
  • 盡可能的減少,用戶點擊搜索結果到跳入app展示內容,這個過程消耗的時間

點擊 Combine APIs to Increase Coverage andImprove the Ranking of Your Results來了解更多關于提升用戶搜索體驗的信息

實戰,對幾個app的分析

淘寶電影

對照一臺沒有安裝過淘寶電影app 的iOS 9.2系統的iPhone6Plus,實驗組也是iPhone6Plus正常使用淘寶電影,訪問頻次2周一次。
試驗過程略
結果:
1.初次安裝未打開的情況下搜索當前上映電影并無結果。
2.啟動過app,停留在列表頁,成功收到數據后再次搜索,可以搜索出當前上映電影,即便該并未進入過電影詳情;斷網后依然可搜索。
3.搜索出的結果,斷網前后并無區別。
4.半年前的電影,兩臺設備都無法搜索出,但是在淘寶電影app內搜索出該電影(無論是在app搜索列表或者詳情頁),spotlight就可以搜索到。

分析:淘寶電影app會在首頁或者之前,將最近數據索引。用戶app內搜索的結果和訪問記錄也會被索引。使用的API應該是NSUserActivity,但是是否將屬性設置為公開,并無結論。

知乎

分析:知乎也是會索引首頁數據和用戶訪問記錄。但是經過斷網測試,首頁數據的緩存可能是隨用戶滑動進行,并不是一次性。

百詞斬

分析:百詞斬的app我用的不多,但是新下載的app,斷網后打開app。然后搜索常用單詞,都可以搜索到。我懷疑百詞斬是直接使用core spotlight 索引了成千上萬個單詞和常用短語。

關于Search API的一些使用場景
1.展示對應新聞 - 今日頭條
2.展示問答帖子 - 知乎
3.展示電影 - 淘寶電影,豆瓣
4.展示電影院 - 淘寶電影
5.展示單詞 - 百詞斬
6.展示藥品 - 用藥助手
7.展示地標 - 百度地圖
具體如何使用Search APIs,就要看需求而定了。

疑惑
  • NSUserActivity 設置為public的情況,在測試(斷網前后對比測試)中并未發現具體例子??赡苁菧y試量級較少。
  • 索引數據究竟最多能索引多少?這個我不知道。但是官網上有這樣一句話

Core Spotlight APIs work best when you have no more than a few thousand items.

看樣子,至少幾千個還是運行的妥妥的。所以,看需求就放心的使用吧。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,524評論 5 6
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,829評論 25 708
  • 現在的生活方式沒有讓我過得更好,那不如就換一種活法吧。
    94d5f46e96ad閱讀 168評論 0 0
  • 前言 最近在看并發編程藝術這本書,對看書的一些筆記及個人工作中的總結。 線程池的優勢 Java中的線程池是運用場景...
    二月_春風閱讀 1,504評論 0 0