在 Xamarin 中通过核心聚焦搜索Search with Core Spotlight in Xamarin.iOS

核心聚焦是适用于 iOS 9 的新框架,该框架提供了一个类似数据库的 API,用于添加、编辑或删除应用中内容的链接。Core Spotlight is a new framework for iOS 9 that presents a database-like API to add, edit or delete links to content within your app. 使用核心聚焦添加的项将在 iOS 设备上的聚焦搜索中提供。Items that have been added using Core Spotlight will be available in Spotlight search on the iOS device.

有关可以使用核心聚焦编制索引的内容类型的示例,请查看 Apple 的邮件、邮件、日历和便笺应用。For an example of the types of content that can be indexed using Core Spotlight, look at Apple's Messages, Mail, Calendar and Notes apps. 它们当前都使用核心聚光灯来提供搜索结果。They all currently use Core Spotlight to provide search results.

创建项Creating an Item

下面是一个示例,其中创建了一个项,并使用核心聚光灯为其编制索引:The following is an example of creating an item and indexing it using Core Spotlight:

using CoreSpotlight;
...

// Create attributes to describe an item
var attributes = new CSSearchableItemAttributeSet();
attributes.Title = "App Center Test";
attributes.ContentDescription = "Automatically test your app on 1,000 devices in the cloud.";

// Create item
var item = new CSSearchableItem ("1", "products", attributes);

// Index item
CSSearchableIndex.DefaultSearchableIndex.Index (new CSSearchableItem[]{ item }, (error) => {
    // Successful?
    if (error !=null) {
        Console.WriteLine(error.LocalizedDescription);
    }
});

此信息将在搜索结果中显示如下:This information would appear like the following in a search result:

还原项Restoring an Item

当用户通过应用的核心聚焦点击添加到搜索结果中的项时,将调用 AppDelegate 方法 ContinueUserActivity (此方法还用于 NSUserActivity)。When the user taps on an item added to the Search result via Core Spotlight for your app, the AppDelegate method ContinueUserActivity is called (this method is also used for NSUserActivity). 例如: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 == CSSearchableItem.ActionType.ToString ()) {
            // Display content for searchable item...
        }
        break;
    }

    return true;
}

请注意,这次我们要检查是否有 CSSearchableItem.ActionType``ActivityType 的活动。Note that this time we are check for the activity having an ActivityType of CSSearchableItem.ActionType.

更新项Updating an Item

在某些情况下,我们使用核心聚光灯创建的索引项需要修改,如需要更改标题或缩略图。There might be times when an Index Item we created with Core Spotlight need to be modified, such as a change in title or thumbnail image is required. 若要进行此更改,我们使用的方法与最初创建索引时使用的方法相同。To make this change, we use the same method as was used to initially create the index. 使用与创建项相同的 ID 创建新 CSSearchableItem,并附加包含已修改属性的新 CSSearchableItemAttributeSetWe create a new CSSearchableItem using the same ID as was used to create the item and attach a new CSSearchableItemAttributeSet containing the modified attributes:

将此项写入可搜索索引时,将用新信息更新现有项。When this item is written to the searchable index, the existing item is updated with the new information.

删除项Deleting an Item

核心聚焦提供多种方法,用于在不再需要索引项时将其删除。Core Spotlight provides multiple ways to delete an index item when it is no longer required.

首先,可以按其标识符删除项,例如:First, you can delete an item by its identifier, for example:

// Delete Items by ID
CSSearchableIndex.DefaultSearchableIndex.Delete(new string[]{"1","16"},(error) => {
    // Successful?
    if (error !=null) {
        Console.WriteLine(error.LocalizedDescription);
    }
});

接下来,可以按其域名删除一组索引项。Next, you can delete a group of index items by their domain name. 例如:For example:

// Delete by Domain Name
CSSearchableIndex.DefaultSearchableIndex.DeleteWithDomain(new string[]{"domain-name"},(error) => {
    // Successful?
    if (error !=null) {
        Console.WriteLine(error.LocalizedDescription);
    }
});

最后,您可以删除具有以下代码的所有索引项:Finally, you can delete all Index Items with the following code:

// Delete all index items
CSSearchableIndex.DefaultSearchableIndex.DeleteAll((error) => {
    // Successful?
    if (error !=null) {
        Console.WriteLine(error.LocalizedDescription);
    }
});

其他核心聚焦功能Additional Core Spotlight Features

核心聚焦具有以下功能,可帮助保持索引准确并保持最新状态:Core Spotlight has the following features that help to keep the index accurate and up-to-date:

  • 批处理更新支持–如果你的应用程序需要同时创建或修改大组索引,则可以通过一次调用将整个批处理发送到 CSSearchableIndex 类的 Index 方法。Batch Update Support – If your app needs to create or modify a large group of indexes at the same time, the entire batch can be sent to the Index method of the CSSearchableIndex class in one call.
  • 索引更改做出响应–使用 CSSearchableIndexDelegate 应用可以响应可搜索索引中的更改和通知。Respond to Index Changes – Using the CSSearchableIndexDelegate your app can respond to changes and notifications from the searchable index.
  • 应用数据保护–使用数据保护类,可以对使用核心聚焦添加到可搜索索引的项实施安全性。Apply Data Protection – Using the data protection classes, you can implement security on the items that you add to the searchable index using Core Spotlight.