取得建議搜尋
如要取得搜尋建議,請對 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)