在 Xamarin 中通过 NSUserActivity 进行搜索Search with NSUserActivity in Xamarin.iOS

NSUserActivity 是在 iOS 8 中引入的,用于提供用于提交的数据。NSUserActivity was introduced in iOS 8 and is used to provide the data for Handoff. 它允许你在应用程序的特定部分中创建活动,然后将这些活动传递到其他 iOS 设备上运行的应用程序的另一个实例。It allows you to create activities in specific parts of your app that can then be passed off to another instance of your app running on a different iOS device. 然后,接收设备可以继续在前一设备上启动的活动,并从用户离开的位置继续。The receiving device can then continue the activity started on the previous device, picking up right where the user left off. 有关使用移交的详细信息,请参阅我们的移交文档简介For more information on using Handoff, please see our Introduction to Handoff documentation.

在 iOS 9 的新手中,NSUserActivity 可以通过聚焦搜索和 Safari 进行索引(公开和私下)和搜索。New to iOS 9, NSUserActivity can be indexed (both publicly and privately) and searched from Spotlight Search and Safari. 通过将 NSUserActivity 标记为可搜索并添加可索引的元数据,可以在 iOS 设备上的搜索结果中列出该活动。By marking a NSUserActivity as searchable and adding indexable metadata, the activity can be listed in the search results on the iOS device.

如果用户从应用程序中选择属于某个活动的搜索结果,则将启动该应用程序,并将重新启动该应用程序所描述的活动并向用户显示 NSUserActivityIf the user selects a search result that belongs to an activity from your app, the app will be launched and the activity described by the NSUserActivity will be restarted and presented to the user.

NSUserActivity 的以下属性用于支持应用搜索:The following properties of NSUserActivity are used to support App Search:

  • EligibleForHandoff –如果 true,则可在移交操作中使用此活动。EligibleForHandoff – If true, this activity can be used in a Handoff operation.
  • EligibleForSearch –如果 true,则此活动将添加到设备上的索引并显示在搜索结果中。EligibleForSearch – If true, this activity will be added to the on-device index and presented in search results.
  • EligibleForPublicIndexing –如果 true,则会将此活动添加到 Apple 的基于云的索引,并向用户显示尚未在其 iOS 设备上安装应用的用户(通过搜索)。EligibleForPublicIndexing – If true, this activity will be added to Apple's cloud based index and presented to users (via search) that have not already installed your app on their iOS device. 有关更多详细信息,请参阅下面的公共搜索索引部分。See the Public Search Indexing section below for more details.
  • Title –为活动提供标题,并将其显示在搜索结果中。Title – Provides a title for your activity and is displayed in the search results. 用户还可以搜索标题的文本。Users can also search for the text of the title itself.
  • Keywords –是一个字符串数组,用于描述你的活动,这些活动将由最终用户进行编制索引和搜索。Keywords – Is an array of strings used to describe your activity that will be indexed and made searchable by the end user.
  • ContentAttributeSetCSSearchableItemAttributeSet 用于进一步详细描述活动,并在搜索结果中提供丰富内容。ContentAttributeSet – Is a CSSearchableItemAttributeSet used to further describe your activity in detail and provide rich content in the search results.
  • ExpirationDate –如果你希望某个活动仅显示到指定日期,则可以在此处提供该日期。ExpirationDate – If you want an activity to only be shown up to a given date, you can provide that date here.
  • WebpageURL –如果可以在 web 上查看活动,或者如果你的应用支持 Safari 的深层链接,你可以在此处设置要访问的链接。WebpageURL – If the activity can be viewed on the web or if your app supports Safari's deep links, you can set the link to visit here.

NSUserActivity 快速入门NSUserActivity Quickstart

按照以下说明在应用程序中实现可搜索的 NSUserActivityFollow these instructions to implement a searchable NSUserActivity in your app:

使用 NSUserActivity 使内容可供搜索,还有一些其他优点There are some additional benefits to using NSUserActivity to make your content searchable.

创建活动类型标识符Creating Activity Type Identifiers

在您可以创建搜索活动之前,您需要创建一个_活动类型标识符_来识别它。Before you can create a Search Activity, you'll need to create an Activity Type Identifier to identify it. 活动类型标识符是添加到应用info.plist文件的 NSUserActivityTypes 数组的短字符串,用于唯一标识给定的用户活动类型。The Activity Type Identifier is a short string added to the NSUserActivityTypes array of the app's Info.plist file used to uniquely identify a given User Activity Type. 对于应用程序支持的每个活动,数组中将有一个条目,并将其公开给应用搜索。There will be one entry in the array for each activity that the app supports and exposes to App Search.

Apple 建议对活动类型标识符使用反向 DNS 样式表示法,以避免冲突。Apple suggests using a reverse-DNS-style notation for the Activity Type Identifier to avoid collisions. 例如: com.company-name.appname.activity 适用于特定应用程序的活动,或可跨多个应用运行的活动 com.company-name.activityFor example: com.company-name.appname.activity for specific app based activities or com.company-name.activity for activities that can run across multiple apps.

创建 NSUserActivity 实例时,将使用活动类型标识符来标识活动的类型。The Activity Type Identifier is used when creating a NSUserActivity instance to identify the type of activity. 当用户点击搜索结果时,如果某个活动继续出现,则活动类型(以及应用的团队 ID)将确定要启动哪个应用以继续执行该活动。When an activity is continued as the result of the user tapping a search result, the Activity Type (along with the app’s Team ID) determines which app to launch to continue the activity.

若要创建所需的活动类型标识符以支持此行为,请编辑info.plist文件,并切换到 "" 视图。To create the required Activity Type Identifiers to support this behavior, edit the Info.plist file and switch to the Source view. 添加 NSUserActivityTypes 键,并使用以下格式创建标识符:Add a NSUserActivityTypes key and create identifiers in the following format:

在上面的示例中,我们为搜索活动创建了一个新的活动类型标识符(com.xamarin.platform)。In the above example, we created one new Activity Type Identifier for the search activity (com.xamarin.platform). 创建自己的应用时,将 NSUserActivityTypes 数组的内容替换为特定于应用支持的活动的活动类型标识符。When creating your own apps, replace the contents of the NSUserActivityTypes array with the Activity Type Identifiers specific to the activities your app supports.

创建活动Creating an Activity

下面是为设备上索引托管搜索创建活动的示例:The following is an example of creating an activity for a on-device index hosted search:

// Create App Search Activity
var activity = new NSUserActivity ("com.xamarin.platform");

// Define details
var info = new NSMutableDictionary ();
info.Add(new NSString("link"),new NSString("http://xamarin.com/platform"));

// Populate Activity
activity.Title = "The Xamarin Platform";
activity.UserInfo = info;

// Add App Search ability
activity.EligibleForSearch = true;
activity.BecomeCurrent();

可以通过设置 NSUserActivityContentAttributeSet 属性来添加更多详细信息,如下所示:We could add further details by setting the ContentAttributeSet property of our NSUserActivity as follows:

通过使用 ContentAttributeSet 可以创建丰富的搜索结果,以便诱使最终用户与之交互。By using a ContentAttributeSet you can create rich search results that entice the end user to interact with them.

响应活动Responding to an Activity

若要响应用户对应用程序点击搜索结果(NSUserActivity),请编辑AppDelegate.cs文件并重写 ContinueUserActivity 方法。To respond to the user tapping on a search result (NSUserActivity) for our app, 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;
    }

    return true;
}

请注意,这是用于响应移交请求的相同方法重写。Note that this is the same method override used to respond to Handoff requests. 现在,如果用户在聚焦搜索结果中单击应用的链接,则会将应用置于前台(或启动(如果尚未运行),并且将显示该链接所表示的内容、导航或功能:Now if the user clicks on a link from our app in the Spotlight Search results, our app will be brought to the foreground (or started if not already running) and the content, navigation or feature represented by that link will be displayed:

公共搜索索引Public Search Indexing

如前文所述,iOS 9 使你可以轻松地提供对用户已在给定 iOS 设备上发现并使用的应用内容和功能的搜索访问。As we saw above, iOS 9 makes it easy to provide search access to app content and features that the user has already discovered and used on a given iOS device. 使用公共索引,iOS 9 为尚未发现内容或功能的用户(或者尚未安装应用的用户)提供了一种方法,用于在搜索中获得这些结果。With Public Indexing, iOS 9 provides a way for users who haven't discovered content or features yet (or who haven't even installed the app) to get those results in their searches too.

公共索引的工作方式如下:Public Indexing works in the following way:

  1. 为应用程序创建活动时,可以将其标记为 "公共"。When you create an activity for your app you can mark it as public.
  2. 公共活动发送到 Apple 并在云中编制索引。Public activities are sent to Apple and indexed in the cloud.
  3. 当更多用户与设备上的应用进行交互并使用活动所表示的特定功能或内容时,它会按级别提升。As more users interact with your app on a device and use the specific feature or content represented by the activity, it rises in rank.
  4. 即使未安装该应用程序,也会向其他用户提供常用公共结果。Popular public results will be available to other users, even if they don't have the app installed.
  5. 这些公共结果将显示在聚焦搜索和 Safari 中(如果活动包括 URL)。These public results will show up in Spotlight Search and Safari (if the activity includes a URL).

我们可以执行前面创建的专用搜索活动,并将其展开为公共:We can take the private search activity that we created above, and expand it to be public:

// Create App Search Activity
var activity = new NSUserActivity ("com.xamarin.platform");

// Define details
var info = new NSMutableDictionary ();
info.Add(new NSString("link"),new NSString("http://xamarin.com/platform"));

// Populate Activity
activity.Title = "The Xamarin Platform";
activity.UserInfo = info;

// Add App Search ability
activity.EligibleForSearch = true;
activity.EligibleForPublicIndexing = true;
activity.BecomeCurrent();

由于已通过设置 EligibleForPublicIndexing = true为公共索引设置了活动,因此它并不意味着会自动添加到 Apple 的公有云索引。Just because an activity has been set for public indexing by setting EligibleForPublicIndexing = true, it doesn't mean that it will be automatically added to Apple's public cloud index. 必须首先满足以下条件:The following conditions must be met first:

  1. 它必须出现在搜索结果中,并被多个用户选择。It must appear in search results and be selected by many users. 在达到活动参与阈值之前,结果将保持为私有。The results remain private until an activity engagement threshold has been met.
  2. 应用设置可防止任何特定于用户的数据被索引并公开。App provisioning prevents any user-specific data from being indexed and made public.

其他权益Additional Benefits

通过在你的应用中通过 NSUserActivity 采用应用搜索,你还可以获得以下功能:By adopting App Search via NSUserActivity in your app, you also get the following features:

  • 移交-由于应用搜索使用与移交相同的机制(NSUserActivity)来公开内容、导航和/或功能,因此你可以轻松地允许应用的用户在一个设备上启动某个活动,并在另一台设备上继续操作。Handoff - Since App Search is exposing content, navigation and/or features using the same mechanism as Handoff (NSUserActivity), you can easily allow your app's users to start an activity on one device and continue it on another.
  • Siri 建议-除了 Siri 建议通常做出的标准建议外,还可自动建议应用中的在职。Siri Suggestions - Along with the standard suggestions that Siri Suggestions normally makes, actives from your app can be automatically suggested.
  • Siri 智能提醒-用户将能够要求 Siri 提醒用户应用中的活动。Siri Smart Reminders - Users will be able to ask Siri to remind them about activities from your app.