trySilentAuthentication without Google+ button

2019-02-27 14:15发布

问题:

I am implementing Google+ sign in in iOS , I used this code and it works fine

signIn = [GPPSignIn sharedInstance];
signIn.delegate = self;
//signIn.shouldFetchGoogleUserEmail = YES;
signIn.shouldFetchGooglePlusUser = YES;
signIn.clientID = kClientId;
signIn.scopes = [NSArray arrayWithObjects:kGTLAuthScopePlusLogin,nil];
signIn.actions = [NSArray arrayWithObjects:@"http://schemas.google.com/ListenActivity",nil];
[signIn authenticate];

I want to use [signIn trySilentAuthentication] method to doesn't leave up each time user log in but it doesn't work without using google+ button GPPSignInButton

So , what wrong with using this code instead of the one above

  signIn = [GPPSignIn sharedInstance];
signIn.delegate = self;
//signIn.shouldFetchGoogleUserEmail = YES;
signIn.shouldFetchGooglePlusUser = YES;
signIn.clientID = kClientId;
signIn.scopes = [NSArray arrayWithObjects:kGTLAuthScopePlusLogin,nil];
signIn.actions = [NSArray arrayWithObjects:@"http://schemas.google.com/ListenActivity",nil];
[signIn trySilentAuthentication];

Is it possible to use trySilentAuthentication with didSelectRowAtIndexPath ? thanks in advance

回答1:

Yep, for sure. Make sure you are calling [[GPPSignIn sharedInstance] trySilentAuthentication] and not creating a new GPPSignIn instance, and make sure you only call it after you have set up the GPPSignIn instance.

So you would need to split you snippet into two: in some early method (viewWillAppear or even in the app delegate) set the parameters on the sharedInstance, and call trySilentAuthentication. When you get the call from didSelectRowAtIndexPath, then call [[GPPSignIn] sharedInstance] authenticate].

EDIT: To clarify what trySilentAuthentication is for.

When you call authenticate, the user is taken out to Google+ app, Chrome, or Safari to sign in. When they come back, a long lived token is stored in the keychain for the user, as well as a short lived token for making API calls. Calling trySilentAuthentication checks whether the long lived token is in the key chain, and generates a new short lived token. If it succeeds it means the user has signed in to that app on that device before, so you generally will want to respond to that appropriately. If it fails (there is no token) it will just return false, or if it can't generate a short lived token it will call finishedWithAuth:error with the error set. It will never take the user out to another app to authenticate (hence the silent part).

You generally want to call trySilentAuthentication very early in the flow in order to learn the state of the user. That does not mean you can't present other sign-in options to them.

In your case it seems like that if the app restarts you want to show the sign in screen to them. That's fine, if a little unpleasant for the user. What you can do is run trySilentAuthentication early on, but in the finishedWithAuth:error, instead of immediately switching to the next screen, just store the auth object. Then when the user presses the Google+ entry in the table, flip to the next view at that point.