Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

feat(sdk): support latest SDK versions for Android and iOS #764

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 15 additions & 29 deletions README.md
Expand Up @@ -126,6 +126,20 @@ $ cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-googleplus
$ cordova prepare
```

EXTRA VARIABLES:

If you need to install a specific version of `GoogleSignIn` library using pod you can pass it as a variable.
This is optional, if this variable is not set the default version will be used.
```
--variable GOOGLE_SIGN_IN_VERSION="~> 6.2.3"
```

If you need to install a specific version of `GoogleUtilities` library using pod you can pass it as a variable.
This is optional, if this variable is not set the default version will be used.
```
--variable GOOGLE_UTILITIES_VERSION="~> 7.4"
```

IMPORTANT:

* _Please note that `myreversedclientid` is a place holder for the reversed clientId you find in your iOS configuration file. Do not surround this value with quotes. **(iOS only Applications)**_
Expand Down Expand Up @@ -183,13 +197,10 @@ document.addEventListener('deviceready', deviceReady, false);
function deviceReady() {
//I get called when everything's ready for the plugin to be called!
console.log('Device is ready!');
window.plugins.googleplus.trySilentLogin(...);
window.plugins.googleplus.login(...);
}
```

### isAvailable
3/31/16: This method is no longer required to be checked first. It is kept for code orthoganality.

### Login

The login function walks the user through the Google Auth process. All parameters are optional, however there are a few caveats.
Expand Down Expand Up @@ -238,31 +249,6 @@ On Android, the error callback (third argument) receives an error status code if

On iOS, the error callback will include an [NSError localizedDescription](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSError_Class/).

### Try silent login
You can call `trySilentLogin` to check if they're already signed in to the app and sign them in silently if they are.

If it succeeds you will get the same object as the `login` function gets,
but if it fails it will not show the authentication dialog to the user.

Calling `trySilentLogin` is done the same as `login`, except for the function name.
```javascript
window.plugins.googleplus.trySilentLogin(
{
'scopes': '... ', // optional - space-separated list of scopes, If not included or empty, defaults to `profile` and `email`.
'webClientId': 'client id of the web app/server side', // optional - clientId of your Web application from Credentials settings of your project - On Android, this MUST be included to get an idToken. On iOS, it is not required.
'offline': true, // Optional, but requires the webClientId - if set to true the plugin will also return a serverAuthCode, which can be used to grant offline access to a non-Google server
},
function (obj) {
alert(JSON.stringify(obj)); // do something useful instead of alerting
},
function (msg) {
alert('error: ' + msg);
}
);
```

It is strongly recommended that trySilentLogin is implemented with the same options as login, to avoid any potential complications.

### logout
This will clear the OAuth2 token.
``` javascript
Expand Down
17 changes: 10 additions & 7 deletions plugin.xml
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-googleplus"
version="8.5.2">
version="9.0.0">

<name>Google SignIn</name>

Expand Down Expand Up @@ -30,9 +30,10 @@
<!-- android -->
<platform name="android">

<preference name="PLAY_SERVICES_VERSION" default="15.0.1"/>
<framework src="com.google.android.gms:play-services-auth:$PLAY_SERVICES_VERSION" />
<framework src="com.google.android.gms:play-services-identity:$PLAY_SERVICES_VERSION" />
<preference name="PLAY_SERVICES_AUTH_VERSION" default="20.3.0"/>
<preference name="PLAY_SERVICES_IDENTITY_VERSION" default="18.0.1"/>
<framework src="com.google.android.gms:play-services-auth:$PLAY_SERVICES_AUTH_VERSION" />
<framework src="com.google.android.gms:play-services-identity:$PLAY_SERVICES_IDENTITY_VERSION" />

<config-file target="res/xml/config.xml" parent="/*">
<feature name="GooglePlus">
Expand Down Expand Up @@ -102,17 +103,19 @@
<framework src="libz.dylib" weak="true" />

<!-- Google frameworks -->
<preference name="GOOGLE_SIGN_IN_VERSION" default="~> 6.2.3"/>
<preference name="GOOGLE_UTILITIES_VERSION" default="~> 7.4"/>
<podspec>
<config>
<source url="https://cdn.cocoapods.org/"/>
</config>
<pods use-frameworks="true">
<pod name="GoogleSignIn" spec="~> 5.0.2"/>
<pod name="GoogleUtilities" spec="~> 7.2.2"/>
<pod name="GoogleSignIn" spec="$GOOGLE_SIGN_IN_VERSION"/>
<pod name="GoogleUtilities" spec="$GOOGLE_UTILITIES_VERSION"/>
</pods>
</podspec>

<hook type="after_plugin_install" src="hooks/ios/prerequisites.js"/>
<hook type="after_plugin_install" src="hooks/ios/prerequisites.js"/>
</platform>

<!-- browser -->
Expand Down
53 changes: 23 additions & 30 deletions src/android/GooglePlus.java
Expand Up @@ -3,8 +3,6 @@
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
Expand All @@ -24,7 +22,6 @@
import com.google.android.gms.common.api.Scope;

import org.apache.cordova.*;
import org.apache.cordova.engine.SystemWebChromeClient;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -38,6 +35,12 @@
import java.security.MessageDigest;
import android.content.pm.Signature;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

/**
* Originally written by Eddy Verbruggen (http://github.com/EddyVerbruggen/cordova-plugin-googleplus)
* Forked/Duplicated and Modified by PointSource, LLC, 2016.
Expand Down Expand Up @@ -70,9 +73,25 @@ public class GooglePlus extends CordovaPlugin implements GoogleApiClient.OnConne
private GoogleApiClient mGoogleApiClient;
private CallbackContext savedCallbackContext;

private ActivityResultLauncher<Intent> signInActivityLauncher;

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);

AppCompatActivity cordovaActivity = cordova.getActivity();

this.signInActivityLauncher = cordovaActivity.registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
public void onActivityResult(ActivityResult result) {
Log.i(TAG, "One of our activities finished up");
// Call handleSignInResult passing in sign in result object
Intent intent = result.getData();
handleSignInResult(Auth.GoogleSignInApi.getSignInResultFromIntent(intent));
}
});

}

@Override
Expand All @@ -89,7 +108,6 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback

// Tries to Log the user in
Log.i(TAG, "Trying to Log in!");
cordova.setActivityResultCallback(this); //sets this class instance to be an activity result listener
signIn();

} else if (ACTION_TRY_SILENT_LOGIN.equals(action)) {
Expand Down Expand Up @@ -194,7 +212,7 @@ private synchronized void buildGoogleApiClient(JSONObject clientOptions) throws
*/
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(this.mGoogleApiClient);
cordova.getActivity().startActivityForResult(signInIntent, RC_GOOGLEPLUS);
this.signInActivityLauncher.launch(signInIntent);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/**
Expand Down Expand Up @@ -274,31 +292,6 @@ public void onConnectionFailed(ConnectionResult result) {
savedCallbackContext.error(result.getErrorCode());
}

/**
* Listens for and responds to an activity result. If the activity result request code matches our own,
* we know that the sign in Intent that we started has completed.
*
* The result is retrieved and send to the handleSignInResult function.
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent Information returned by the child activity
*/
@Override
public void onActivityResult(int requestCode, final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

Log.i(TAG, "In onActivityResult");

if (requestCode == RC_GOOGLEPLUS) {
Log.i(TAG, "One of our activities finished up");
//Call handleSignInResult passing in sign in result object
handleSignInResult(Auth.GoogleSignInApi.getSignInResultFromIntent(intent));
}
else {
Log.i(TAG, "This wasn't one of our activities");
}
}

/**
* Function for handling the sign in result
Expand Down
5 changes: 1 addition & 4 deletions src/ios/GooglePlus.h
@@ -1,16 +1,13 @@
#import <Cordova/CDVPlugin.h>
#import <GoogleSignIn/GoogleSignIn.h>

@interface GooglePlus : CDVPlugin<GIDSignInDelegate, GIDSignInDelegate>
@interface GooglePlus : CDVPlugin

@property (nonatomic, copy) NSString* callbackId;
@property (nonatomic, assign) BOOL isSigningIn;

- (void) isAvailable:(CDVInvokedUrlCommand*)command;
- (void) login:(CDVInvokedUrlCommand*)command;
- (void) trySilentLogin:(CDVInvokedUrlCommand*)command;
- (void) logout:(CDVInvokedUrlCommand*)command;
- (void) disconnect:(CDVInvokedUrlCommand*)command;
- (void) share_unused:(CDVInvokedUrlCommand*)command;
Comment on lines -9 to -14
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compatibility breakage in iOS:

  • isAvailable: even necessary?
  • trySilentLogin: open to backfills if community still requires it
  • share_unused: seemed to be cruft


@end