Rocky
Rocky

Reputation: 31

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

        }

}

Upvotes: 3

Views: 433

Answers (1)

thomas
thomas

Reputation: 2281

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!

Upvotes: 2

Related Questions