Google Places API警告

时间:2017-10-13 09:48:41

标签: ios swift3 google-api

任何人都可以帮助您在使用Google地方信息时如何在Swift3中解决此警告?

“Places API警告:使用前尚未在GMSAutocompleteResultsViewController实例上设置委托。请注意,这可能会导致未定义的行为,例如无法关闭屏幕,未收到选择通知,以及无法正确管理对象生命周期。“

我基本上遵循https://developers.google.com/places/ios-api/autocomplete中的教程[在搜索顶部添加搜索栏]

代码:

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

        }

}

1 个答案:

答案 0 :(得分:2)

您只需将委托设置为控制器,如下所示:

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted: Bool, error: Error?) in
        if error != nil {
            print(error?.localizedDescription ?? "error requesting notification authorization")
        }
    }
    UIApplication.shared.registerForRemoteNotifications()

那应该摆脱警告。

祝你好运!