Xamarin 中的应用搜索增强功能App Search Enhancements in Xamarin.iOS

本文介绍了 Apple 在 iOS 10 中进行的应用搜索的增强功能,以及如何在 Xamarin 中实现这些功能。This article covers the enhancements Apple has made to App Search in iOS 10 and how to implement them in Xamarin.iOS.

在 iOS 10 中,Apple 已对应用搜索进行了多项改进,例如众包的深层链接、应用内搜索、搜索验证结果的延续和可视化。In iOS 10, Apple has made several enhancements to App Search such as Crowdsourced Deep-Linking, In-App Searching, Search Continuation and Visualization of Validation Results. 本文介绍如何在 Xamarin iOS 应用程序中实现这些功能。This article will cover implementing these features in a Xamarin.iOS app.

关于应用搜索增强功能About 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.

以下部分将更详细地介绍这些主题。The following sections will cover these topics in more detail.

iOS 10 提供一种机制,用于计算常见的深层链接到应用的频率,并使用此信息来改进搜索结果中应用内容的排名,同时仍然使用差分来保护用户的身份隐私iOS 10 provides a mechanism to count the frequency that popular deep-links into an app are followed by the user and uses this information to improve the ranking of an app's content in search results, while still protecting the user's identity by using Differential Privacy.

对于使用 NSUserActivity 对象提供深层链接 Url 并将 EligibleForPublicIndexing 属性设置为 true的应用程序,iOS 10 将差异隐私哈希的一个子集提交到 Apple 的服务器。For app's that use NSUserActivity objects to provide deep-link URLs and have the EligibleForPublicIndexing property set to true, iOS 10 submits a subset of Differential Privacy Hashes to Apple's servers. 然后,将使用此信息在搜索结果中升级常用的应用内内容。This information is then used to promote popular in-app content in search results.

有关在 Xamarin iOS 应用程序中实现深层链接的详细信息,请参阅使用 NSUserActivity 文档进行搜索For more information on implementing deep-linking in a Xamarin.iOS app, please see our Search with NSUserActivity documentation.

应用内搜索In-App Searching

通过实现新的CSSearchQuery类,应用程序可以提供聚光灯的搜索和匹配规则技术来查找自身内部的内容,而无需用户离开应用(类似于邮件、邮件和便笺应用的工作方式)。By implementing the new CSSearchQuery class, an app can provide Spotlight's search and matching rule technology to find content inside of itself, without the user having to leave the app (similar to how the Mail, Messages and Notes app work).

通常,支持 CSSearchQuery 的应用程序不需要维护其自己的单独搜索索引。Typically, apps that support CSSearchQuery will not need to maintain their own, separate search index.

搜索延续Search Continuation

在 iOS 9 中,Apple 引入了搜索 Api (如核心聚焦、NSUserActivity 和 web 标记),以便在应用程序中提供内容的深度,使用户能够使用聚光灯和 Safari 搜索界面搜索该内容。In iOS 9, Apple introduced the Search APIs (such as Core Spotlight, NSUserActivity and web markup) to provide deep-liking of content within an app to allow users to search for that content using both the Spotlight and Safari search interfaces. 有关更多详细信息,请参阅新的搜索 api文档。See our New Search APIs documentation for more details.

在 iOS 10 Apple 中,通过允许用户在聚光灯或 Safari 中开始搜索,然后在打开应用时继续搜索。In iOS 10 Apple builds upon this feature by allowing the user to start a search in Spotlight or Safari, then continue the search when they open an app.

若要实现此功能,请编辑应用的 Info.plist 文件,添加布尔类型的 CoreSpotlightContinuation 密钥,并将其值设置为 YESTo implement this feature, edit the app's Info.plist file, add the CoreSpotlightContinuation key of type Boolean and set its value to YES:

若要响应用户继续搜索结果(NSUserActivity),请编辑 AppDelegate.cs 文件并重写 ContinueUserActivity 方法。To respond to the user continuing a search result (NSUserActivity), edit the AppDelegate.cs file and override the ContinueUserActivity method. 例如:For example:

public override bool ContinueUserActivity (UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
{

    // Take action based on the activity type
    switch (userActivity.ActivityType) {
    case "com.xamarin.platform":
        // Restore the state of the app here...
        break;
    default:
        if (userActivity.ActivityType == CSSearchQuery.ContinuationActionType) {
            var search = userActivity.UserInfo.KeyForValue(CSSearchQuery.QueryString);
            // Continue user's search here...
        }
        break;
    }

    return true;
}

此代码将查找查询继续操作类型(userActivity.ActivityType == CSSearchQuery.ContinuationActionType),然后从 NSUserActivity 类的用户信息字典(userActivity.UserInfo.KeyForValue(CSSearchQuery.QueryString))读取用户的当前查询。This code looks for the query continuation action type (userActivity.ActivityType == CSSearchQuery.ContinuationActionType), then reads the user's current query from the NSUserActivity class's user info dictionary (userActivity.UserInfo.KeyForValue(CSSearchQuery.QueryString)). 在这里,应用需要采取措施来继续执行用户的搜索。From here, the app needs to take action to continue the user's search.

有关在 Xamarin iOS 应用中使用搜索的详细信息,请参阅使用核心聚焦文档进行搜索。For more information on working with searches in a Xamarin.iOS app, please see our Search with Core Spotlight documentation.

验证结果的可视化效果Visualization of Validation Results

Apple 的应用搜索 API 验证工具现在显示网站标记的可视化表示形式,并在执行测试时显示深层链接(包括Schema.org中定义的标记)。Apple's App Search API Validation Tool now displays a visual representation of a website's markup and deep-linking (including markup such as defined at Schema.org) when preforming tests.

通过使用验证工具,开发人员可以查看 Applebot Web 爬网程序为网站编制索引的信息,例如标题、说明、URL 和任何其他受支持的元素。By using the Validation Tool, a developer can see the information that the Applebot Web Crawler has indexed for the site such as title, description, URL and any other supported elements.

有关使用 Web 标记的详细信息,请参阅使用 Web 标记进行搜索文档。For more information on working with Web Markup, please see our Seach with Web Markup documentation.

消息应用图像共享Message App Image Sharing

如果消息应用扩展提供了用于在消息中共享的映像,则可以将该扩展配置为允许用户在消息中执行热门图像的聚焦搜索,而无需离开应用。If a Message App Extension provides images for sharing in Messages, the extension can be configured to allow the user to perform Spotlight searches for popular images from within the Messages, without having to leave the app.

若要启用此功能,请执行以下操作:To enable this feature, do the following:

  1. 创建消息应用扩展。Create a Message App Extension.
  2. com.apple.developer.associated-domains 添加到应用的权利,并包括托管消息应用扩展所共享的映像的 web 域的列表。Add the com.apple.developer.associated-domains to the app's entitlements and include a list of web domains that host the images the Message App Extension is sharing. 为每个域指定 spotlight-image-search 服务。For each domain, specify the spotlight-image-search service.
  3. apple-app-site-association 文件添加到承载图像的网站。Add a apple-app-site-association file to the website that is hosting the images. 此文件包含 spotlight-image-search 服务的字典,并包含应用的 ID,该 ID 是团队 ID 或应用 ID 前缀,后跟捆绑 ID。This file includes a dictionary for the spotlight-image-search service and includes the app's ID, which is the Team ID or App ID prefix followed by the Bundle ID. 该文件最多可包含500个路径和模式,这些路径和模式将按聚光灯编制索引,并包含在常用图像搜索中。The file can contain up to 500 paths and patterns that will be indexed by Spotlight and included in popular image searches. 有关详细信息,请参阅 Apple 的创建和上传关联文件文档。For more information, please see Apple's Creating and Uploading the Association File documentation.
  4. 允许 Applebot 对网站爬网。Allow the Applebot to crawl the websites. 有关 Applebot文档,请参阅 Apple 的相关文档。Please see Apple's About Applebot documentation.

有关更多详细信息,请参阅我们的消息应用集成文档。See our Message App Integration documentation for more details.

总结Summary

本文介绍了 Apple 在 iOS 10 中进行的应用搜索的增强功能,以及如何在 Xamarin 中实现这些功能。This article has covered the enhancements Apple has made to App Search in iOS 10 and how to implement them in Xamarin.iOS.