エラー内容
PlacesAPIを使用し、オートコンプリートを機能を実装したところ、以下のerrorメッセージとともにautocompleteControllerが強制終了してしまいます
Error: Error Domain=com.google.places.ErrorDomain Code=-4 "Operation failed due to an invalid (malformed or missing) API key. See the developer's guide (https://developers.google.com/places/ios-sdk/start) for information on creating and using an API key." UserInfo={NSLocalizedFailureReason=Operation failed due to an invalid (malformed or missing) API key. See the developer's guide (https://developers.google.com/places/ios-sdk/start) for information on creating and using an API key., NSUnderlyingError=0x281f85410 {Error Domain=com.google.places.api.server.ErrorDomain Code=-2 "REQUEST_DENIED" UserInfo={NSLocalizedFailureReason=You must enable Billing on the Google Cloud Project at https://console.cloud.google.com/project/_/billing/enable Learn more at https://developers.google.com/maps/gmp-get-started, NSLocalizedDescription=REQUEST_DENIED}}}
試したこと
GoogleCloudPlatformに請求先アカウントを追加
APIキーの制限として、iosアプリのみ、バンドルIDを設定済み
AppDelegate.swift
import UIKit import GoogleMaps import GooglePlaces @main class AppDelegate: UIResponder, UIApplicationDelegate { let cGoogleMapsAPIKey = "***" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. GMSPlacesClient.provideAPIKey(cGoogleMapsAPIKey) GMSServices.provideAPIKey(cGoogleMapsAPIKey) return true } // MARK: UISceneSession Lifecycle func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { // Called when the user discards a scene session. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } }
実装中のコード
import UIKit import GooglePlaces class AddTicketViewController: UIViewController { @IBOutlet weak var addressTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() } @IBAction func addressTap(_ sender: Any) { let autocompleteController = GMSAutocompleteViewController() autocompleteController.delegate = self // オートコンプリート用のViewの表示 present(autocompleteController, animated: true, completion: nil) } func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) } } //PlaceAPI extension AddTicketViewController: GMSAutocompleteViewControllerDelegate { // オートコンプリートで場所が選択した時に呼ばれる関数 func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) { // 名前をoutletに設定 addressTextField.text = place.formattedAddress print(place) 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) { // TODO: handle the error. print("Error: (error)") dismiss(animated: true, completion: nil) } // User cancelled the operation. func wasCancelled(_ viewController: GMSAutocompleteViewController) { print("Autocomplete was cancelled.") dismiss(animated: true, completion: nil) } }
こちらの記事を参考に実装しました
https://qiita.com/ron214_ron/items/5149ce7c5578dc0d72ce
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。