搜索内容

获取搜索内容

我们在 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];
    }

下一步:显示搜索结果