UIPhotoSearchSuggestion
An input suggestion that carries photo search metadata for people, subjects, locations, and time periods.
Declaration
class UIPhotoSearchSuggestionDiscussion
When someone types text that could match a photo library search, such as “photos from Paris last summer,” the system recognizes the input as a photo library search and delivers a UIPhotoSearchSuggestion through the textField(_:insertInputSuggestion:) or textView(_:insertInputSuggestion:) delegate method. Use as? UIPhotoSearchSuggestion to check whether the incoming UIInputSuggestion is a photo search suggestion and access its metadata.
After receiving a suggestion, you have two options: Pass the object directly to the Photos framework to present a pre-populated photo picker, or read the whoValues, whatValues, whereValues, and whenValues arrays to build a custom search experience.
You can’t create a UIPhotoSearchSuggestion directly. The system creates and delivers instances through the input suggestion system.
Presenting a photo picker
Pass the suggestion to PHPickerSearchText(photoSearchSuggestion:) to pre-populate a PHPickerViewController with photos matching the person’s search.
class SearchViewController: UIViewController, UITextFieldDelegate, PHPickerViewControllerDelegate {
@IBOutlet var searchField: UITextField!
func textField(_ textField: UITextField,
insertInputSuggestion inputSuggestion: UIInputSuggestion) {
if let photoSuggestion = inputSuggestion as? UIPhotoSearchSuggestion {
presentPhotosPicker(with: photoSuggestion)
}
}
func presentPhotosPicker(with suggestion: UIPhotoSearchSuggestion) {
var configuration = PHPickerConfiguration()
configuration.searchText = PHPickerSearchText(photoSearchSuggestion: suggestion)
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true)
}
func picker(_ picker: PHPickerViewController,
didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true)
// Handle selected photos.
}
}Building a custom search
If your app has its own photo search UI, read the filter arrays and construct your own query.
func textField(_ textField: UITextField,
insertInputSuggestion inputSuggestion: UIInputSuggestion) {
guard let suggestion = inputSuggestion as? UIPhotoSearchSuggestion else { return }
// Build a custom query from the individual filter values.
let who = suggestion.whoValues // e.g., ["John"]
let what = suggestion.whatValues // e.g., ["hiking"]
let locations = suggestion.whereValues // e.g., ["Paris"]
let timeframes = suggestion.whenValues // e.g., ["last summer"]
performCustomPhotoSearch(people: who, subjects: what, locations: locations, timeframes: timeframes)
}