Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places API Warning

Can anyone help how to solve this warning in Swift3 while using Google Places?

"Places API warning: A delegate has not been set on an instance of GMSAutocompleteResultsViewController before use. Note that this may result in undefined behavior such as being unable to dismiss screens, not being notified of selections, and being unable to correctly manage object lifecycle."

I basically followed the tutorial in https://developers.google.com/places/ios-api/autocomplete [Add a search bar to the top of a view]

Code:

import UIKit[enter image description here][1]
import GooglePlaces

class LocationTabViewController: UIViewController {        
        var resultsViewController: GMSAutocompleteResultsViewController?
        var searchController: UISearchController?
        var resultView: UITextView?

        override func viewDidLoad() {
            super.viewDidLoad()

            resultsViewController = GMSAutocompleteResultsViewController()

            resultsViewController?.delegate = self as? GMSAutocompleteResultsViewControllerDelegate

            searchController = UISearchController(searchResultsController: resultsViewController)

            searchController?.searchResultsUpdater = resultsViewController

            let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))

            subView.addSubview((searchController?.searchBar)!)

            view.addSubview(subView)

            searchController?.searchBar.sizeToFit()

            searchController?.hidesNavigationBarDuringPresentation = false

           // When UISearchController presents the results view, present it in

            // this view controller, not one further up the chain.

            definesPresentationContext = true

        }

    }



    // Handle the user's selection.

    extension ViewController: GMSAutocompleteResultsViewControllerDelegate {

     func resultsController(_ resultsController: GMSAutocompleteResultsViewController,

                               didAutocompleteWith place: GMSPlace) {

            // Do something with the selected place.

            print("Place name: \(place.name)")

            print("Place address: \(String(describing: place.formattedAddress))")

            print("Place attributions: \(String(describing: place.attributions))")

        }



        func resultsController(_ resultsController: GMSAutocompleteResultsViewController,

                               didFailAutocompleteWithError error: Error){

            // TODO: handle the error.

            print("Error: ", error.localizedDescription)

        }



        // Turn the network activity indicator on and off again.

        func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {

            UIApplication.shared.isNetworkActivityIndicatorVisible = true

        }



        func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {

            UIApplication.shared.isNetworkActivityIndicatorVisible = false

        }

}
like image 599
Rocky Avatar asked Jan 23 '26 08:01

Rocky


1 Answers

You just need to set the delegate to the controller, like this:

resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self

That should get rid of the warning.

Good luck!

like image 140
Thomas Wang Avatar answered Jan 25 '26 20:01

Thomas Wang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!