Tìm kiếm nội dung

Nhận nội dung tìm kiếm

Chúng tôi đã thêm tính năng hỗ trợ lấy giao diện người dùng nội dung tìm kiếm trong SDK. Search Content là một thuật ngữ chung đại diện cho nhiều loại tính năng nội dung. Hãy xem các phần sau để biết cách yêu cầu từng loại tính năng nội dung tương ứng.

Lặp lại tìm kiếm

Gọi phương thức getSearchContent trên ContextualSearchRuntime. Phương thức này lấy một GetSearchContentOptions duy nhất mà bạn có thể chỉ định văn bản để sử dụng nhằm nhận nội dung tìm kiếm. Để lặp lại cụm từ tìm kiếm, hãy chuyển văn bản lặp lại nội dung tìm kiếm vào thực thể GetSearchContentOptions.

Đối tượng SearchContents được trả về không chứa bất kỳ thuộc tính nào có thể truy cập. Tệp này chứa tất cả thông tin cần thiết để hiển thị nội dung tìm kiếm, nhưng nếu không thì sẽ không rõ ràng.

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.
                                    }];

Hiển thị nội dung tìm kiếm

Để hiển thị nội dung tìm kiếm, hãy sử dụng các phương thức sau.

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];
    }

Tiếp theo: Hiện kết quả tìm kiếm