Google SignIn in iOS - logout and log back in gives error
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!
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.
It happens all the time.
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?
Yes, it is going to actual web browser for sign in. I am using iOS 9.0.2
Any help from google is appreciated. Our app submission is blocked because of this issue. Thanks!
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
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
}
I have the exact problem and with your code it doesn't work either
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/
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!
Hello! I find out that problem gone away if you implement GIDSignInDelegate in AppDelegate, strange but works for me.