I have already set the clientID ,scope and then on button click in MyViewController ,i am calling method login from LoginCLass which works but after [signIn authenticate]
You Can use
if (![signIn trySilentAuthentication])
[signIn authenticate];
Follow this link for more detail
trySilentAuthentication without Google+ button
The problem seems to be where an instance of the GPPSignIn is not persisted between leaving the app to load Safari and coming back to your app.
Currently in your login method you have:
GPPSignIn *signIn = [GPPSignIn sharedInstance];
so this is a local instance variable. Try moving this to the class level:
@implementation LoginClass {
GPPSignIn *signIn;
}
then use that in your login method
Here is the simple solution for this.
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if (url.absoluteString.range(of: "oauth2callback") != nil) {
GPPSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: nil)
}
return false
}
Since your LoginClass is a Swift class, the fix is to make it a subclass of NSObject.
I had the same issue: when I tried to set the delegate to a class that was not a subclass of NSObject, the assignment failed and the delegate remained nil. When I added NSObject as a superclass, the assignment worked as expected.
This happened to me too in my apps running iOS 8. What did it for me was to set the clientId in the AppDelegate as soon as the app launched, and not in the viewDidLoad method of my UIViewController class as indicated in the Google+ Sign-in for iOS example in the following URL: https://developers.google.com/+/mobile/ios/sign-in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/
//Google+
// Set app's client ID for |GPPSignIn| and |GPPShare|.
[GPPSignIn sharedInstance].clientID = @"xxxxxx.......apps.googleusercontent.com";
...
return YES;
}
So, in your UIViewController class the sign-in method should be:
- (void)viewDidLoad {
[super viewDidLoad];
//Google+ for Logging in the user again if the app has been authorized
signIn = [GPPSignIn sharedInstance];
signIn.shouldFetchGooglePlusUser = YES;
signIn.shouldFetchGoogleUserID = YES;
signIn.shouldFetchGoogleUserEmail = YES;
signIn.scopes = @[ kGTLAuthScopePlusLogin ];
signIn.delegate = self;
[signIn trySilentAuthentication];
...
}