搜尋內容

取得搜尋內容

我們已在 SDK 中新增支援功能,可取得搜尋內容 UI。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];
    }

下一篇:顯示搜尋結果