google-services icon indicating copy to clipboard operation
google-services copied to clipboard

Google SignIn in iOS - logout and log back in gives error

Open guna opened this issue 10 years ago • 11 comments

I have implemented google SignIn in my iOS app and can login to google account for the first time. But when I logout and try to log back in, I get an error:

"The user canceled the sign-in flow."

I have not cancelled my sign-in flow. I clicked on "Allow" for permission to access email address and basic profile info and then clicked on "Done" in the safari window. Sometimes I get a keychain_error.

The code is give below.

-(void)viewDidLoad
{
   self.signIn = [GIDSignIn sharedInstance];
    [self.signIn setScopes:[NSArray arrayWithObject:@"profile"]];
    self.signIn.uiDelegate = self;
    self.signIn.delegate = self;
    self.signIn.allowsSignInWithBrowser = TRUE;
    self.signIn.allowsSignInWithWebView = TRUE;
    self.signIn.clientID = kClientID;

}

-(void)googleSignInButtonClicked`
{
  if([GIDSignIn sharedInstance].hasAuthInKeychain) {
      GIDGoogleUser *user = [GIDSignIn sharedInstance].currentUser;
      if(!user) {
         [[GIDSignIn sharedInstance] signInSilently];
      }
  } else {
      [self.signIn signIn];
  }
}

I have spend awful amount of time on this. Any thoughts are greatly appreciated. Thanks much!

guna avatar Feb 08 '16 20:02 guna

Is this happening while the device is being debugged, or at all times? There have been some reports of an issue with keychain access when XCode is attached.

ianbarber avatar Feb 09 '16 02:02 ianbarber

It happens all the time.

guna avatar Feb 09 '16 05:02 guna

Hmm, the clicking on "done" seems wrong as I re-read you comment. You also mention the Safari window - is it going to the actual web browser for the sign in? If so, which version of iOS are you using?

ianbarber avatar Feb 09 '16 16:02 ianbarber

Yes, it is going to actual web browser for sign in. I am using iOS 9.0.2

guna avatar Feb 10 '16 00:02 guna

Any help from google is appreciated. Our app submission is blocked because of this issue. Thanks!

guna avatar Feb 10 '16 01:02 guna

I have very similar issue.

I have empty iOS application with added GIDSigInButton When i click on this button SafariView pops up with google login after accepting permission func application(application: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool is called but func handleURL(url: NSURL!, sourceApplication: String!, annotation: AnyObject!) -> Bool returns false. I assume there is some problem with url:NSURL or maybe application configuration but handleURL function dont't report what is the problem.

And i can't find way to debug it.

Here is my AppDelegate for clarification

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Initialize sign-in
        var configureError: NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")

        GIDSignIn.sharedInstance().delegate = self

        return true
    }

    func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
      // url    NSURL   "com.googleusercontent.apps.XXXXXXX-jvsn0fmpba89s7p4g36hxxxxxxxxxxxx:/oauth2callback?code=4/yyRcEY1d_lnXNa7bpxxxxxxxxxxxxxxxxx#"
     // sourceApplication: com.apple.SafariViewService

        let result = GIDSignIn.sharedInstance()
            .handleURL(url,
            sourceApplication: String(options[UIApplicationOpenURLOptionsSourceApplicationKey]),
            annotation: options[UIApplicationOpenURLOptionsAnnotationKey])

        return result // result is false even if url appears to have valid url inside
    }

After call exists url handler. Safari view hangs for few seconds and then redirect to google homepage. Where i can click in on top left corner on "done" and return to application. Console then says

"The user canceled the sign-in flow."

I use 9.2 in simulator

encero avatar Mar 11 '16 09:03 encero

OK...

So when i was poking around values that are passed to openUrl handler for comment i just wrote. I find that String(options[UIApplicationOpenURLOptionsSourceApplicationKey]) is in fact Optional(String) even if xcode says otherwise.

let applicationSource = String(options[UIApplicationOpenURLOptionsSourceApplicationKey])

print(applicationSource) // Optional(com.apple.SafariViewService)

// Xcode thinks applicationSource is string even if print says otherwise
print(applicationSource!) // Cannot force unwrap value of non-optional type 'String'

// added unwrapping before conversion to string solves everything
let unwrapedApplicationSource = String(options[UIApplicationOpenURLOptionsSourceApplicationKey]!)

print(unwrapedApplicationSource) // com.apple.SafariViewService

After that discovery and making that small change to my handler everything is so far working as expected

func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
        let result = GIDSignIn.sharedInstance()
            .handleURL(url,
            //added exclamation mark
            sourceApplication: String(options[UIApplicationOpenURLOptionsSourceApplicationKey]!),
            annotation: options[UIApplicationOpenURLOptionsAnnotationKey])

        return result // now result is true and everything is working
    }

EDIT: after more googling this pops out https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html

basically my assumption that String(AnyObjectHere) is valid type cast to string from AnyObject is wrong. Correct type cast for this is use as! construction.

final version

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

        return result
    }

encero avatar Mar 11 '16 10:03 encero

I have the exact problem and with your code it doesn't work either

batiatto avatar Mar 30 '16 21:03 batiatto

I got the same issue. You should clearly indicate the GIDButton class should be assigned to UIView property in https://developers.google.com/identity/sign-in/ios/sdk/

It takes me a little bit while to figure out what is the trigger.

eg: https://www.appcoda.com/google-sign-in-how-to/

hellohelloye avatar Aug 08 '16 19:08 hellohelloye

I'm going to close this as obsolete as I don't believe we have this issue in the latest version - please flag or reopen if still experiencing though!

ianbarber avatar Dec 06 '16 21:12 ianbarber

Hello! I find out that problem gone away if you implement GIDSignInDelegate in AppDelegate, strange but works for me.

RomanTikhonychev avatar Feb 05 '17 23:02 RomanTikhonychev