NewbieWantsToMaster NewbieWantsToMaster - 2 months ago 10
iOS Question

iOS Google Sign In error

I'm trying to implement a google sign in button, when I add the following lines to my AppDelegate.swift file, I get this error:

Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String'

Any ideas what's wrong with this code? By the way, this code is just copied and pasted from google page at:
https://developers.google.com/identity/sign-in/ios/sign-in?configured&ver=swift

func application(application: UIApplication,
openURL url: NSURL, options options: [String: AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey],
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}


iOS Google Sign In error

Thanks

Answer

handleURL is looking for arguments of type "String" for sourceApplication and annotation, but options is providing "AnyObject." Casting those dictionary values to "String" oughtta do it.

Try this:

func application(application: UIApplication,
    openURL url: NSURL, options: [String: AnyObject]) -> Bool {
        return GIDSignIn.sharedInstance().handleURL(url,
            sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
            annotation: options[UIApplicationOpenURLOptionsAnnotationKey] as? String)
}

This took me forever, too. Hope that helps!