AutocompleteWithSearchViewController.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2016 Google LLC. All rights reserved.
  3. *
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  6. * file except in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under
  11. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. * ANY KIND, either express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #import "GooglePlacesDemos/Samples/Autocomplete/AutocompleteWithSearchViewController.h"
  16. #import <GooglePlaces/GooglePlaces.h>
  17. NSString *const kSearchBarAccessibilityIdentifier = @"searchBarAccessibilityIdentifier";
  18. @interface AutocompleteWithSearchViewController () <GMSAutocompleteResultsViewControllerDelegate,
  19. UISearchBarDelegate>
  20. @end
  21. @implementation AutocompleteWithSearchViewController {
  22. UISearchController *_searchController;
  23. GMSAutocompleteResultsViewController *_acViewController;
  24. }
  25. + (NSString *)demoTitle {
  26. return NSLocalizedString(
  27. @"Demo.Title.Autocomplete.UISearchController",
  28. @"Title of the UISearchController autocomplete demo for display in a list or nav header");
  29. }
  30. #pragma mark - View Lifecycle
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. _acViewController = [[GMSAutocompleteResultsViewController alloc] init];
  34. _acViewController.autocompleteFilter = self.autocompleteFilter;
  35. _acViewController.placeFields = self.placeFields;
  36. _acViewController.delegate = self;
  37. _searchController =
  38. [[UISearchController alloc] initWithSearchResultsController:_acViewController];
  39. _searchController.hidesNavigationBarDuringPresentation = NO;
  40. _searchController.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  41. _searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
  42. _searchController.searchBar.delegate = self;
  43. _searchController.searchBar.accessibilityIdentifier = kSearchBarAccessibilityIdentifier;
  44. [_searchController.searchBar sizeToFit];
  45. self.navigationItem.titleView = _searchController.searchBar;
  46. self.definesPresentationContext = YES;
  47. // Work around a UISearchController bug that doesn't reposition the table view correctly when
  48. // rotating to landscape.
  49. self.edgesForExtendedLayout = UIRectEdgeAll;
  50. self.extendedLayoutIncludesOpaqueBars = YES;
  51. _searchController.searchResultsUpdater = _acViewController;
  52. if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  53. _searchController.modalPresentationStyle = UIModalPresentationPopover;
  54. } else {
  55. _searchController.modalPresentationStyle = UIModalPresentationFullScreen;
  56. }
  57. }
  58. #pragma mark - UISearcBarDelegate
  59. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  60. // Inform user that the autocomplete query has been cancelled and dismiss the search bar.
  61. [_searchController setActive:NO];
  62. [_searchController.searchBar setHidden:YES];
  63. [self autocompleteDidCancel];
  64. }
  65. #pragma mark - GMSAutocompleteResultsViewControllerDelegate
  66. - (void)resultsController:(GMSAutocompleteResultsViewController *)resultsController
  67. didAutocompleteWithPlace:(GMSPlace *)place {
  68. // Display the results and dismiss the search controller.
  69. [_searchController setActive:NO];
  70. [self autocompleteDidSelectPlace:place];
  71. }
  72. - (void)resultsController:(GMSAutocompleteResultsViewController *)resultsController
  73. didFailAutocompleteWithError:(NSError *)error {
  74. // Display the error and dismiss the search controller.
  75. [_searchController setActive:NO];
  76. [self autocompleteDidFail:error];
  77. }
  78. // Show and hide the network activity indicator when we start/stop loading results.
  79. - (void)didRequestAutocompletePredictionsForResultsController:
  80. (GMSAutocompleteResultsViewController *)resultsController {
  81. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  82. // Reset the text and photos view when we are requesting for predictions.
  83. [self resetViews];
  84. }
  85. - (void)didUpdateAutocompletePredictionsForResultsController:
  86. (GMSAutocompleteResultsViewController *)resultsController {
  87. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  88. }
  89. @end