取得建議搜尋
如要取得搜尋建議,請呼叫 ContextualSearchRuntime 上的 getSearchSuggestions 方法。這個方法會採用單一 GetSearchSuggestionsOptions,您可以在其中指定要用於取得搜尋建議的文字。
傳回的 SearchSuggestions 物件不含任何可存取的屬性。其中包含顯示搜尋建議的所有必要資訊,但其他部分則不透明。
Swift 使用 Swift 並行樣式,Objective-C 則使用回呼函式。
Swift
getSearchSuggestions(withOptions:)
Task {
let suggestions = try await searchRuntime.getSearchSuggestions(
withOptions: GetSearchSuggestionsOptions(withTextContext:['Sample Query', 'Another query string']))
}
Objective-C
getSearchSuggestionsWithOptions
[searchRuntime
getSearchSuggestionsWithOptions:
[[GetSearchSuggestionsOptions alloc] initWithTextContext: @[@'Sample Query', @'Another query string' ]]
completionHandler:^(SearchSuggestions *_Nullable suggestions,
NSError *_Nullable error) {
// error will be not null if there is an error.
// On success, suggestions will be non null.
}];
取得根據位置提供的建議搜尋
getSearchSuggestions 要求也支援根據位置提供搜尋建議。使用 LocationSuggestionsContext 建構 GetSearchSuggestionsOptions 物件。
請按照下列步驟建構 LocationSuggestionsContext 類別:
- (必要) 將
latitude、longitude和radiusMeters設為與要求區域相符。 - (選用) 設定
startTime,以符合搜尋建議應套用的要求時間。 - (選用) 使用要求的地理類型設定
geoTypes清單。系統會根據提供的類型調整服務傳回結果的排名和篩選條件。支援的值記錄在「LocationSuggestionsContext」中。
Swift
getSearchSuggestions(withOptions:)
Task {
let locationContext: LocationSuggestionsContext = {
let context = LocationSuggestionsContext()
context.latitude = 40.748595
context.longitude = -73.992365
context.radiusMeters = 2500
context.startTime = try?
Date.ISO8601FormatStyle.iso8601.parse("2024-03-14T19:20:30.000+02:00")
context.geoTypes = ["restaurant", "cafe"]
return context
}()
let suggestions = try await searchRuntime.getSearchSuggestions(
withOptions: GetSearchSuggestionsOptions(locationContext: [
locationContext
]))
}
Objective-C
getSearchSuggestionsWithOptions
LocationSuggestionsContext *locationContext;
locationContext = [[LocationSuggestionsContext alloc] init];
locationContext.latitude = 40.748595;
locationContext.longitude = -73.992365;
locationContext.radiusMeters = @2500;
[searchRuntime
getSearchSuggestionsWithOptions:
[[GetSearchSuggestionsOptions alloc] initWithLocationContext: locationContext]
completionHandler:^(SearchSuggestions *_Nullable suggestions,
NSError *_Nullable error) {
// error will be not null if there is an error.
// On success, suggestions will be non null.
}];
多媒體
如要顯示搜尋建議晶片,請使用下列方法 (如果您使用 UIKit 與 SwiftUI,樣式會有所不同)。
Swift UIKit
使用 SearchSuggestionsViewController 顯示搜尋建議方塊。
// Create the SearchSuggestionsViewController with specified display options.
var searchSuggestionsController = SearchSuggestionsViewController(options: viewOptions)
...
// Add the view controller to the existing view hierarchy
searchSuggestionsController.addToViewContoller(self)
view.addSubview(searchSuggestionsController.view)
...
// Assign a specific set of search suggestions to this view controller.
searchSuggestionsController.searchSuggestions = suggestions
Objective-C
使用 SearchSuggestionsViewController 顯示搜尋建議方塊。
// Create the SearchSuggestionsController with specified display options.
SearchSuggestionsViewController *_searchSuggestionsController =
[[SearchSuggestionsViewController alloc] initWithOptions:_customizationOptions];
...
// Add the view controller to the existing view hierarchy
[_searchSuggestionsController addToViewController:self];
[self.view addSubview:_searchSuggestionsController.view];
...
// Assign a specific set of search suggestions to this view controller.
_searchSuggestionsController.searchSuggestions = suggestions
SwiftUI
搜尋建議可透過 SearchSuggestionsView 顯示。
SearchSuggestionsView(suggestions: suggestions, options: options)