搜索内容

获取搜索内容

我们已在 SDK 中添加了对获取搜索内容界面的支持。 Search Content是一个通用术语,代表多种类型的内容 功能,请查看以下部分,了解如何分别请求每种类型的内容 功能。

搜索重复内容

ContextualSearchRuntime 调用 getSearchContent 方法。此方法采用单个 GetSearchContentOptions,您可以在其中指定用于获取搜索内容的文本。如需获取搜索重复内容,请将搜索重复内容文本传入 GetSearchContentOptions 实例。

返回的 SearchContents 对象不包含任何可访问的属性。 它包含呈现搜索内容所需的所有信息,但在其他方面是不透明的。

SwiftUI

Task {
  let sampleSearchRepeat = "search repeat text"
  let options = GetSearchContentOptions(searchRepeat: sampleSearchRepeat)
  let searchContents: SearchContents =
    try await contextualSearchRuntime.getSearchContent(withOptions: options)
}

Objective-C

GetSearchContentOptions *options =
    [[GetSearchContentOptions alloc] initWithSearchRepeat:@"sample search repeat"];
[_contextualSearchRuntime getSearchContentWithOptions:options
                                    completionHandler:^(SearchContents *_Nullable contents,
                                                        NSError *_Nullable error) {
                                      // error will be not null if there is an error.
                                      // On success, contents will be non null.
                                    }];

显示搜索内容

如需显示搜索内容,请使用以下方法。

SwiftUI

var customizationOptions = SearchContentViewOptions()
...
VStack {
  ForEach(searchContents.searchContentBlocks) { block in
    SearchContentView(blockData: block, options: customizationOptions)
  }
}

Objective-C

for (SearchContentBlockData *block in ((SearchContents *)_searchContents)
             .searchContentBlocks) {
      SearchContentViewController *blockView = [[SearchContentViewController alloc]
          initWithOptions:[[SearchSuggestionsViewOptions alloc] init]];
      blockView.blockData = block;
      [_searchContentStackView addSubview:blockView.view];
      [blockView addToViewController:self];
    }

下一步:显示搜索结果