From df694c27e9e34a5ec64e0a75da4962099816c765 Mon Sep 17 00:00:00 2001 From: Massimiliano Coppola <41947432+Zeno97@users.noreply.github.com> Date: Sun, 29 Dec 2024 14:12:42 +0100 Subject: [PATCH] Update GooglePlus.m --- src/ios/GooglePlus.m | 161 +++++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 74 deletions(-) diff --git a/src/ios/GooglePlus.m b/src/ios/GooglePlus.m index 9e68985e..cc203991 100644 --- a/src/ios/GooglePlus.m +++ b/src/ios/GooglePlus.m @@ -39,14 +39,83 @@ - (void) isAvailable:(CDVInvokedUrlCommand*)command { } - (void) login:(CDVInvokedUrlCommand*)command { - [[self getGIDSignInObject:command] signIn]; + NSDictionary* options = command.arguments[0]; + NSString *reversedClientId = [self getreversedClientId]; + NSString *clientId = [self reverseUrlScheme:reversedClientId]; + + NSString* scopesString = options[@"scopes"]; + NSString* serverClientId = options[@"webClientId"]; + NSString *loginHint = options[@"loginHint"]; + BOOL offline = [options[@"offline"] boolValue]; + NSArray* scopes = nil; + if (scopesString != nil) { + scopes = [scopesString componentsSeparatedByString:@" "]; + //[signIn setScopes:scopes]; + } + [[self getGIDSignInObject:command] signInWithPresentingViewController:self.viewController hint:loginHint additionalScopes:scopes completion:^(GIDSignInResult * _Nullable signInResult, NSError * _Nullable error) { + if (error) { + CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.localizedDescription]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self->_callbackId]; + } else { + NSString *email = signInResult.user.profile.email; + NSString *idToken = signInResult.user.idToken.tokenString; + NSString *accessToken = signInResult.user.accessToken.tokenString; + NSString *refreshToken = signInResult.user.refreshToken.tokenString; + NSString *userId = signInResult.user.userID; + NSString *serverAuthCode = signInResult.serverAuthCode != nil ? signInResult.serverAuthCode : @""; + NSURL *imageUrl = [signInResult.user.profile imageURLWithDimension:120]; // TODO pass in img size as param, and try to sync with Android + NSDictionary *result = @{ + @"email" : email, + @"idToken" : idToken, + @"serverAuthCode" : serverAuthCode, + @"accessToken" : accessToken, + @"refreshToken" : refreshToken, + @"userId" : userId, + @"displayName" : signInResult.user.profile.name ? : [NSNull null], + @"givenName" : signInResult.user.profile.givenName ? : [NSNull null], + @"familyName" : signInResult.user.profile.familyName ? : [NSNull null], + @"imageUrl" : imageUrl ? imageUrl.absoluteString : [NSNull null], + }; + + CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self->_callbackId]; + } + }]; } /** Get Google Sign-In object @date July 19, 2015 */ - (void) trySilentLogin:(CDVInvokedUrlCommand*)command { - [[self getGIDSignInObject:command] restorePreviousSignIn]; + [[self getGIDSignInObject:command] restorePreviousSignInWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) { + if (error) { + CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.localizedDescription]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self->_callbackId]; + } else { + NSString *email = user.profile.email; + NSString *idToken = user.idToken.tokenString; + NSString *accessToken = user.accessToken.tokenString; + NSString *refreshToken = user.refreshToken.tokenString; + NSString *userId = user.userID; + //NSString *serverAuthCode = signInResult.serverAuthCode != nil ? signInResult.serverAuthCode : @""; + NSURL *imageUrl = [user.profile imageURLWithDimension:120]; // TODO pass in img size as param, and try to sync with Android + NSDictionary *result = @{ + @"email" : email, + @"idToken" : idToken, + @"serverAuthCode" : @"",//serverAuthCode + @"accessToken" : accessToken, + @"refreshToken" : refreshToken, + @"userId" : userId, + @"displayName" : user.profile.name ? : [NSNull null], + @"givenName" : user.profile.givenName ? : [NSNull null], + @"familyName" : user.profile.familyName ? : [NSNull null], + @"imageUrl" : imageUrl ? imageUrl.absoluteString : [NSNull null], + }; + + CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self->_callbackId]; + } + }]; } /** Get Google Sign-In object @@ -54,6 +123,7 @@ - (void) trySilentLogin:(CDVInvokedUrlCommand*)command { @date updated March 15, 2015 (@author PointSource,LLC) */ - (GIDSignIn*) getGIDSignInObject:(CDVInvokedUrlCommand*)command { + //global assignment for completion _callbackId = command.callbackId; NSDictionary* options = command.arguments[0]; NSString *reversedClientId = [self getreversedClientId]; @@ -66,34 +136,20 @@ - (GIDSignIn*) getGIDSignInObject:(CDVInvokedUrlCommand*)command { NSString *clientId = [self reverseUrlScheme:reversedClientId]; - NSString* scopesString = options[@"scopes"]; NSString* serverClientId = options[@"webClientId"]; - NSString *loginHint = options[@"loginHint"]; BOOL offline = [options[@"offline"] boolValue]; NSString* hostedDomain = options[@"hostedDomain"]; GIDSignIn *signIn = [GIDSignIn sharedInstance]; - signIn.clientID = clientId; - - [signIn setLoginHint:loginHint]; - + GIDConfiguration *configuration = [[GIDConfiguration alloc] initWithClientID:clientId]; if (serverClientId != nil && offline) { - signIn.serverClientID = serverClientId; - } - - if (hostedDomain != nil) { - signIn.hostedDomain = hostedDomain; - } - - signIn.presentingViewController = self.viewController; - signIn.delegate = self; - - // default scopes are email and profile - if (scopesString != nil) { - NSArray* scopes = [scopesString componentsSeparatedByString:@" "]; - [signIn setScopes:scopes]; + configuration = [[GIDConfiguration alloc] initWithClientID:clientId serverClientID:serverClientId hostedDomain:hostedDomain openIDRealm:nil]; } + + [signIn setConfiguration:configuration]; + + return signIn; } @@ -128,62 +184,19 @@ - (void) logout:(CDVInvokedUrlCommand*)command { } - (void) disconnect:(CDVInvokedUrlCommand*)command { - [[GIDSignIn sharedInstance] disconnect]; - CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"disconnected"]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [[GIDSignIn sharedInstance] disconnectWithCompletion:^(NSError * _Nullable error) { + CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"disconnected"]; + if (error != nil) { + NSLog(@"Failed to disconnect: %@", error.localizedDescription); + CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.localizedDescription]; + } + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; } - (void) share_unused:(CDVInvokedUrlCommand*)command { // for a rainy day.. see for a (limited) example https://github.com/vleango/GooglePlus-PhoneGap-iOS/blob/master/src/ios/GPlus.m } -#pragma mark - GIDSignInDelegate -/** Google Sign-In SDK - @date July 19, 2015 - */ -- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error { - if (error) { - CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.localizedDescription]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:_callbackId]; - } else { - NSString *email = user.profile.email; - NSString *idToken = user.authentication.idToken; - NSString *accessToken = user.authentication.accessToken; - NSString *refreshToken = user.authentication.refreshToken; - NSString *userId = user.userID; - NSString *serverAuthCode = user.serverAuthCode != nil ? user.serverAuthCode : @""; - NSURL *imageUrl = [user.profile imageURLWithDimension:120]; // TODO pass in img size as param, and try to sync with Android - NSDictionary *result = @{ - @"email" : email, - @"idToken" : idToken, - @"serverAuthCode" : serverAuthCode, - @"accessToken" : accessToken, - @"refreshToken" : refreshToken, - @"userId" : userId, - @"displayName" : user.profile.name ? : [NSNull null], - @"givenName" : user.profile.givenName ? : [NSNull null], - @"familyName" : user.profile.familyName ? : [NSNull null], - @"imageUrl" : imageUrl ? imageUrl.absoluteString : [NSNull null], - }; - - CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:_callbackId]; - } -} - -/** Google Sign-In SDK - @date July 19, 2015 - */ -- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController { - self.isSigningIn = YES; - [self.viewController presentViewController:viewController animated:YES completion:nil]; -} - -/** Google Sign-In SDK - @date July 19, 2015 - */ -- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController { - [self.viewController dismissViewControllerAnimated:YES completion:nil]; -} - @end