Search code examples
iosswift3google-places-apigmsautocomplete

Swift 3 iOS -Google GMSAutoComplete malfunctioning since upgrade


Error caption:

enter image description here

I'm using Google GMSAutocompleteViewController.

I have my API key in AppDelegate, the most recent pod for Swift 3 Xcode 8, the import GoogleMaps and import GooglePlacesAPI are at the top of the vc and in appDelegate, and my internet connection is fine.

When I was using Swift 2.2 whenever I typed in an address everything worked fine. Now that I upgraded to Swift 3 every now and then I'll make a mistake on an address, then the "Can't load search results" cloud caption appears. When I dismiss the vc and try again I keep getting the same caption. I tried shutting the app off and restarting the simulator but it won't let me access autocomplete services. I'm way under the 2500 free daily limit search because it's literally only me testing the app.

I also notice the Google AutoComplete docs says to use GMSPlacesClient.provideAPIKey("...") but the the GMSPlacesClient method provideAPIKey isn't available so I only use GMSServices.provideAPIKey(".."). I wonder is this part of the issue.

Does anyone know the fix for this issue?

AppDelegate:

import UIKit
import GoogleMaps
import GooglePlacesAPI
import SystemConfiguration

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    override init() {
        super.init()

        //Google Maps API Key
        GMSServices.provideAPIKey("WORKS")

        GMSPlacesClient.provideAPIKey("DOES NOT WORK")
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

my code from googlePlaces:

import UIKit
import GooglePlaces
import GoogleMaps

class ViewController: UIViewController {


  @IBAction func onLaunchClicked(sender: UIButton) {
    let acController = GMSAutocompleteViewController()
    acController.delegate = self
    present(acController, animated: true, completion: nil)
  }
}

extension ViewController: GMSAutocompleteViewControllerDelegate {

  func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    print("Place name: \(place.name)")
    print("Place address: \(place.formattedAddress)")
    print("Place attributions: \(place.attributions)")
    dismiss(animated: true, completion: nil)
  }

  func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    print(".....\(error)")
    dismiss(animated: true, completion: nil)
  }

  func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    dismiss(animated: true, completion: nil)
  }
}

Solution

  • You should put GooglePlaces, GooglePlacePicker and GoogleMaps into your Podfile:

    pod 'GooglePlaces'
    pod 'GooglePlacePicker'
    pod 'GoogleMaps'
    

    And provide your API_KEY:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        GMSPlacesClient.provideAPIKey("API_KEY_HERE")
    
        return true
    }
    

    enter image description here

    For more information, follow this guide: https://developers.google.com/places/ios-api/start