Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 760
NewbieWantsToMaster Avatar asked Dec 06 '15 22:12

NewbieWantsToMaster


People also ask

Why I can't sign in my Google Account on my iPhone?

Update your device to the latest version of iOS or iPadOS. You can update your device wirelessly by going to Settings > General > Software Update and if there is an update, follow the onscreen instructions to update your device. Clear the history, cache and cookies to see if this fixes your problem.

Why am I getting a sign in error on Google?

Your sign-in may be blocked if the device or location you're using is new. Try again from a device or location that you commonly sign in from. Sign in to your Google Account on the new device and try again the following week.

How do I fix Google sign in failed?

How can you fix Google sign-in error on android phone? Some of the solutions to this error include disabling the 2-step verification process, removing and adding your Google account, and restoring your device to its factory settings.

How do I sign into Google on iOS?

On your iPhone or iPad, open the Safari app. Go to www.google.com. Tap your profile image or Sign in. Follow the sign-in steps.


2 Answers

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:

Update: Swift 3

func application(_ application: UIApplication,
                 open url: URL, options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool
   return GIDSignIn.sharedInstance().handle(url,
       sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
       annotation: options[UIApplicationOpenURLOptionsKey.annotation] as? String)
}

Swift 2

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!

like image 69
lonesomewhistle Avatar answered Nov 08 '22 15:11

lonesomewhistle


Xcode 8 Swift 3

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
  return GIDSignIn.sharedInstance().handle(
  url,
  sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
  annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
like image 39
pickaxe Avatar answered Nov 08 '22 17:11

pickaxe