在 Xamarin 中搜索 ApiSearch APIs in Xamarin.iOS

本文介绍如何使用 iOS 9 提供的应用搜索 Api,以允许用户在 Xamarin iOS 应用中搜索信息和功能。This article covers using the App Search APIs provided by iOS 9 to allow users to search for information and features inside your Xamarin.iOS apps.

已在 iOS 9 中展开搜索,以提供更好的新方法来访问 Xamarin iOS 应用中的信息和功能。Search has been expanded in iOS 9 to provide great new ways to access information and features inside a Xamarin.iOS app. 使用新的应用搜索 Api,可通过聚焦和 Safari 搜索结果、移交和 Siri 提醒和建议使应用内容可供搜索。Using the new App Search APIs, app content is made searchable through Spotlight and Safari search results, Handoff and Siri Reminders and Suggestions. 这样,用户就可以快速访问应用中的活动和信息。This allows users to quickly access activities and information deep within your app.

此外,新的搜索 Api 使您可以更轻松地在应用程序中集成搜索,而无需事先搜索实现。Additionally, the new Search APIs make it easier to integrate search in your app without prior search implementation experience. 因此,Apple 声称,通常需要几个小时才能使用应用搜索来统一搜索 iOS 9 应用的内容。Because of this, Apple claims that it typically takes a few hours to make an iOS 9 app's content universally searchable using App Search.

应用搜索由三个单独的 Api 组成:App Search is composed of three separate APIs:

  1. NSUserActivity -这是 Apple 在 iOS 8 中发布的移交 API 的扩展。NSUserActivity - This is an extension of the Handoff API that Apple released in iOS 8. 它用于使应用交互历史记录由用户以公共方式和私下方式进行搜索。It is used to make app interaction history searchable both publicly and privately) by the user.

  2. 核心聚焦-允许应用对其内容编制索引,使其显示在搜索结果中。Core Spotlight - Allows an app to index its content to be presented in search results. 它的工作方式类似于数据库 API,可在其中添加和删除项,这是在应用程序中对私有内容进行索引的最佳方式。It works like a database API where items can be added and removed and it is the best way to index private content within an app.

  3. WebMarkup -用于通过 web 界面提供对其内容的访问的应用(不仅从应用内)。WebMarkup - For apps that provide access to their content via a web interface (not only from within the app). Web 内容可以使用将由 Apple 爬网的特殊链接进行标记,并在用户的 iOS 9 设备上提供与你的应用程序的深层链接。Web content can be marked up with special links that will be crawled by Apple and provide deep linking to your app on the user's iOS 9 device.

选择应用搜索方法Selecting an App Search Approach

确定要实现的这些方法中的哪一种取决于您的应用程序提供的交互类型及其提供的内容类型。Deciding which of these methods to implement depends on the types of interaction provided by your app and the type of content it presents.

使用以下准则:Use the following guidelines:

  • NSUserActivity –使用此框架可为公共内容和专用内容提供可搜索性,并在应用中同时可搜索性导航点。NSUserActivity – Use this framework to provide searchability for both public and private content and also searchability of navigation points within your app.

  • 核心聚焦–使用此框架为存储在设备上的专用数据提供可搜索性。Core Spotlight – Use this framework to provide searchability for private data stored on the device.

  • Web 标记–使用此框架为应用程序提供可搜索性,这些应用程序不仅可以从应用内提供,还可以从应用的网站提供。Web Markup – Use this framework to provide searchability for apps that present their content not only from within the app, but from the app's website as well.

每个应用搜索方法都是独特的,可以单独使用,但是 Apple 设计它们一起工作。Each of the App Search approaches are distinct and can be used individually, however Apple designed them to work together. 使用多种方法来索引特定项时,请确保对每种方法使用相同的项 ID ,以便单个链接一起工作。When using more than one approach to index a specific item, ensure that you use the same Item ID on each approach, so that individual links work together.

使用多种方法不仅可以确保最终用户能够找到您的内容,而且还有助于改进您在搜索范围内的项目排名。Using more than one approach not only ensures that your content will be found by the end user but also helps to improve your item's ranking from within search.

虽然排名过程对开发人员而言是透明的,但用户与给定项目的交互对此排名(例如,用户轻按了链接)进行了很大的权衡。While the ranking process in mostly transparent to the developer, user interaction with a given item weighs heavily upon this rank (for example the user taping a link). 通过提供丰富的信息性项,可以确保用户 enticed 与内容进行交互,从而提高其排名。By providing rich, informative items, you can ensure that a user will be enticed to interact with your content, thus raising its ranking.

要编制索引的内容What Content to Index

Apple 提供以下建议,其中包含在你的应用中为提供搜索索引的内容和操作:Apple provides the following suggestions as to what content and actions to provide search indexes for in your app:

  • 用户从应用程序中查看、创建或特选的任何内容。Any content viewed, created or curated by the user from within your app.
  • 应用中的导航点和功能。Navigation points and features within the app.
  • 最近下载到设备的应用显示的新消息、内容或其他类型的项目。Things like new messages, content or other types of items displayed by your app that have recently been downloaded to the device.

应用搜索增强功能App Search Enhancements

IOS 10 中的核心聚焦为应用搜索提供了多项增强功能,例如:Core Spotlight in iOS 10 provides several enhancements to App Search such as:

  • 众包深层链接的热门程度(带有差异隐私) -提供一种在搜索结果中升级深层链接的应用内容的方法。Crowdsourced Deep-Link Popularity (with differential privacy) - Provides a way to promote deep-linked app content in search results.
  • 应用内搜索-使用新的 CSSearchQuery 类提供应用内聚焦搜索功能,类似于邮件、邮件和便笺应用的工作方式。In-App Searching - Use the new CSSearchQuery class to provide in-app Spotlight search ability similar to how the Mail, Messages and Notes apps work.
  • 搜索延续-允许用户在聚焦或 Safari 中开始搜索,然后打开应用并继续搜索。Search Continuation - Allows a user to start a search in Spotlight or Safari, then open an app and continue that search.
  • 验证结果的可视化效果-Apple 的应用搜索 API 验证工具现在会在执行测试时显示网站标记和深层链接的可视表示形式。Visualization of Validation Results - Apple's App Search API Validation Tool now displays a visual representation of a website's markup and deep-linking when preforming tests.
  • 消息应用程序映像共享-允许使用常用的应用程序内映像在消息中共享(通过消息应用扩展),以便在聚焦搜索中显示。Message App Image Sharing - Allows popular in-app images provided for sharing in Messages (via a Message App Extension) to appear in Spotlight searches.

若要了解详细信息,请参阅应用搜索增强指南。To find out more, please see our App Search Enhancements guide.

主动建议Proactive Suggestions

iOS 10 提供了新的方法,可让系统在适当的时间自动向用户提供有用的信息,从而促进应用程序的参与。iOS 10 presents new ways of driving engagement to an app by allowing the system to proactively present helpful information automatically to the user at appropriate times. 正如 iOS 9 提供的功能,可以使用聚光灯、移交和 Siri 建议以及 iOS 10 向应用添加深层搜索,应用程序可以从以下位置公开系统可向用户显示的功能:Just as iOS 9 provided the ability to add deep search to the app using Spotlight, Handoff and Siri Suggestions, with iOS 10 an app can expose functionality that can be presented to the user by the system from within the following locations:

  • 应用切换器The App Switcher
  • 锁定屏幕The Lock Screen
  • CarPlayCarPlay
  • 映射Maps
  • Siri 交互Siri Interactions
  • QuickType 建议QuickType Suggestions

应用使用NSUserActivity、web 标记、核心聚焦、MapKit、Media Player 和 UIKit 等技术集合向系统公开此功能。An app exposes this functionality to the system using a collection of technologies such as NSUserActivity, web markup, Core Spotlight, MapKit, Media Player and UIKit.

若要了解详细信息,请参阅前瞻性建议指南。To find out more, please see our Proactive Suggestions guide.

总结Summary

本文介绍了 iOS 9 为 Xamarin iOS 应用提供的新搜索 API 功能。This article has covered the new Search API features that iOS 9 provides for Xamarin.iOS apps. 它涵盖了NSUserActivity、用于索引内容的核心聚光灯Web 标记方法。It covered NSUserActivity, Core Spotlight and Web Markup methods for indexing content. 完成时,只需简短地讨论何时应使用给定的搜索方法和应编制索引的内容类型。It finished with a short discussion of when a given search approach should be used and what types of content should be indexed.