搜尋內容

取得搜尋內容

我們已在 SDK 中新增支援,可取得搜尋內容 UI。Search Content 是指多種內容功能的一般用語,請參閱下列各節,瞭解如何分別要求每種內容功能。

搜尋重複

呼叫 ContextualSearchRuntimegetSearchContent 方法。這個方法會採用單一 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];
    }

下一步:顯示搜尋結果