6

I am developing an iOS app using Google Maps API for IOS. And I installed the CocoaPod for my project and configure them according to tutorial on Google Developer. However, when I run my project, it says

*** Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must > be initialized via [GMSServices provideAPIKey:...] prior to use'

But I already call "GMSServices.provideAPIKey on the AppDelegate.swift. Following is the code:

....
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    GMSServices.provideAPIKey("***********************")
    return true
}
....

(**************) is my API Key.

And because Google Maps API use Objective C, so I created a Bridging Header to import the library.

I tried to set breakpoint on [application:didFinishLaunchingWithOption]. But it will raise exception before run that function, which I think is very weird.

So confused about it. Thanks in advance.

1
  • The exception happen only if you didnt include GMSServices.provideAPIKey() in your didFinishLaunchingWithOptions method, you should paste all the lines from your AppDelegate file. Commented Sep 28, 2015 at 18:17

4 Answers 4

5

Problem finally solved, the reason is that I initialize a fields using Google Maps library in the one model class and it will be created before the app run. So this error happens. When I moved this variable into the method, problem solved. Following is the code that causes error:

class PlaceManager {
    let placeClient = GMSPlacesClient()
    ...
    func getSuggestions(queryString:String) -> [String]{
        ...
    }
}

After

class PlaceManager {
    func getSuggestions(queryString:String) -> [String]{
        let placeClient = GMSPlacesClient()
        ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Instead of didFinishLaunchingWithOptions, move the call to willFinishLaunchingWithOptions. This method is called after state restoration has occurred but before your app’s window and other UI have been presented. (Which in your case might be a UI that consumes Google Map API)

func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
GMSServices.provideAPIKey("***********************")

    return true
}

Comments

2

I just had the same problem. I created a GMSMapView object and it was initialized before maybe the api key could be read. So I moved it inside the viewDidLoad method and problem solved.

Before :

class ViewController: ..... {

let mapView = GMSMapView()

After :

class ViewController: ..... {

var mapView : GMSMapView?

override viewDidLoad(){
    mapView = GMSMapView()

2 Comments

This is the same response that someone else already gave. Consider upvoting that answer rather than adding your own.
This is it. This solved my problem when I already have an API key attached to didFinishLaunchingWithOptions
0

In your appDelegate.m at didFinishLaunchingWithOptions this add these GMSService line with api key for me it worked.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [GMSServices provideAPIKey:@"**************"];
  [FIRApp configure];
}

and add this import at the top

#import <GoogleMaps/GoogleMaps.h>

1 Comment

I don't think this addresses the original 'GMSServicesException'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.