获取搜索建议
如需获取搜索建议,请对 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)