Skip to content

Commit 970beb2

Browse files
committed
Refractor iOS adapter
1 parent aac32d9 commit 970beb2

File tree

3 files changed

+73
-62
lines changed

3 files changed

+73
-62
lines changed

GoogleSignInPlugin/Assets/Plugins/iOS/GoogleSignIn/GoogleSignIn.h

+21-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,26 @@
1414
* limitations under the License.
1515
*/
1616
#import <GoogleSignIn/GIDSignIn.h>
17-
@interface GoogleSignInHandler
18-
: NSObject <GIDSignInDelegate, GIDSignInUIDelegate>
17+
#import <GoogleSignIn/GIDConfiguration.h>
18+
19+
@interface GoogleSignInHandler : NSObject
20+
{
21+
@public
22+
GIDConfiguration* signInConfiguration;
23+
24+
@public
25+
NSString* loginHint;
26+
27+
@public
28+
NSMutableArray* additionalScopes;
29+
}
30+
31+
@property(class, nonatomic, readonly) GoogleSignInHandler *sharedInstance;
32+
33+
- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController;
34+
35+
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)_error;
36+
37+
- (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)_error;
1938

2039
@end

GoogleSignInPlugin/Assets/Plugins/iOS/GoogleSignIn/GoogleSignIn.mm

+49-28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#import <GoogleSignIn/GIDGoogleUser.h>
1919
#import <GoogleSignIn/GIDProfileData.h>
2020
#import <GoogleSignIn/GIDSignIn.h>
21+
#import <UnityAppController.h>
22+
23+
#import "UnityInterface.h"
2124

2225
#import <memory>
2326

@@ -59,6 +62,19 @@ void UnpauseUnityPlayer() {
5962

6063
@implementation GoogleSignInHandler
6164

65+
GIDConfiguration* signInConfiguration = nil;
66+
NSString* loginHint = nil;
67+
NSMutableArray* additionalScopes = nil;
68+
69+
+ (GoogleSignInHandler *)sharedInstance {
70+
static dispatch_once_t once;
71+
static GoogleSignInHandler *sharedInstance;
72+
dispatch_once(&once, ^{
73+
sharedInstance = [self alloc];
74+
});
75+
return sharedInstance;
76+
}
77+
6278
/**
6379
* Overload the presenting of the UI so we can pause the Unity player.
6480
*/
@@ -105,9 +121,6 @@ - (void)signIn:(GIDSignIn *)signIn
105121
case kGIDSignInErrorCodeKeychain:
106122
currentResult_->result_code = kStatusCodeInternalError;
107123
break;
108-
case kGIDSignInErrorCodeNoSignInHandlersInstalled:
109-
currentResult_->result_code = kStatusCodeDeveloperError;
110-
break;
111124
case kGIDSignInErrorCodeHasNoAuthInKeychain:
112125
currentResult_->result_code = kStatusCodeError;
113126
break;
@@ -146,6 +159,7 @@ - (void)signIn:(GIDSignIn *)signIn
146159
* The parameters are intended to be primative, easy to marshall.
147160
*/
148161
extern "C" {
162+
149163
/**
150164
* This method does nothing in the iOS implementation. It is here
151165
* to make the API uniform between Android and iOS.
@@ -168,31 +182,29 @@ bool GoogleSignIn_Configure(void *unused, bool useGameSignIn,
168182
bool requestIdToken, bool hidePopups,
169183
const char **additionalScopes, int scopeCount,
170184
const char *accountName) {
171-
if (webClientId) {
172-
[GIDSignIn sharedInstance].serverClientID =
173-
[NSString stringWithUTF8String:webClientId];
174-
}
175-
176-
[GIDSignIn sharedInstance].shouldFetchBasicProfile = true;
177-
178-
int scopeSize = scopeCount;
179-
180-
if (scopeSize) {
181-
NSMutableArray *tmpary =
182-
[[NSMutableArray alloc] initWithCapacity:scopeSize];
183-
for (int i = 0; i < scopeCount; i++) {
184-
[tmpary addObject:[NSString stringWithUTF8String:additionalScopes[i]]];
185+
NSString *path = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];
186+
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
187+
NSString *clientId = [dict objectForKey:@"CLIENT_ID"];
188+
GIDConfiguration* config = [[GIDConfiguration alloc] initWithClientID:clientId];
189+
if (webClientId) {
190+
config = [[GIDConfiguration alloc] initWithClientID:clientId serverClientID:[NSString stringWithUTF8String:webClientId]];
191+
}
192+
[GoogleSignInHandler sharedInstance]->signInConfiguration = config;
193+
194+
int scopeSize = scopeCount;
195+
if (scopeSize) {
196+
NSMutableArray *tmpary = [[NSMutableArray alloc] initWithCapacity:scopeSize];
197+
for (int i = 0; i < scopeCount; i++) {
198+
[tmpary addObject:[NSString stringWithUTF8String:additionalScopes[i]]];
199+
}
200+
[GoogleSignInHandler sharedInstance]->additionalScopes = tmpary;
185201
}
186202

187-
[GIDSignIn sharedInstance].scopes = tmpary;
188-
}
189-
190-
if (accountName) {
191-
[GIDSignIn sharedInstance].loginHint =
192-
[NSString stringWithUTF8String:accountName];
193-
}
203+
if (accountName) {
204+
[GoogleSignInHandler sharedInstance]->loginHint = [NSString stringWithUTF8String:accountName];
205+
}
194206

195-
return !useGameSignIn;
207+
return !useGameSignIn;
196208
}
197209

198210
/**
@@ -226,7 +238,12 @@ bool GoogleSignIn_Configure(void *unused, bool useGameSignIn,
226238
void *GoogleSignIn_SignIn() {
227239
SignInResult *result = startSignIn();
228240
if (!result) {
229-
[[GIDSignIn sharedInstance] signIn];
241+
[[GIDSignIn sharedInstance] signInWithConfiguration:[GoogleSignInHandler sharedInstance]->signInConfiguration
242+
presentingViewController:UnityGetGLViewController()
243+
hint:[GoogleSignInHandler sharedInstance]->loginHint
244+
callback:^(GIDGoogleUser *user, NSError *error) {
245+
[[GoogleSignInHandler sharedInstance] signIn:[GIDSignIn sharedInstance] didSignInForUser:user withError:error];
246+
}];
230247
result = currentResult_.get();
231248
}
232249
return result;
@@ -239,7 +256,9 @@ bool GoogleSignIn_Configure(void *unused, bool useGameSignIn,
239256
void *GoogleSignIn_SignInSilently() {
240257
SignInResult *result = startSignIn();
241258
if (!result) {
242-
[[GIDSignIn sharedInstance] signInSilently];
259+
[[GIDSignIn sharedInstance] restorePreviousSignInWithCallback:^(GIDGoogleUser *user, NSError *error) {
260+
[[GoogleSignInHandler sharedInstance] signIn:[GIDSignIn sharedInstance] didSignInForUser:user withError:error];
261+
}];
243262
result = currentResult_.get();
244263
}
245264
return result;
@@ -252,7 +271,9 @@ void GoogleSignIn_Signout() {
252271

253272
void GoogleSignIn_Disconnect() {
254273
GIDSignIn *signIn = [GIDSignIn sharedInstance];
255-
[signIn disconnect];
274+
[signIn disconnectWithCallback:^(NSError *error) {
275+
[[GoogleSignInHandler sharedInstance] signIn:[GIDSignIn sharedInstance] didDisconnectWithUser:nil withError:error];
276+
}];
256277
}
257278

258279
bool GoogleSignIn_Pending(SignInResult *result) {

GoogleSignInPlugin/Assets/Plugins/iOS/GoogleSignIn/GoogleSignInAppController.mm

+3-32
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,6 @@ + (void)load {
6262
- (BOOL)GoogleSignInAppController:(UIApplication *)application
6363
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
6464

65-
// IMPORTANT: IF you are not supplying a GoogleService-Info.plist in your
66-
// project that contains the client id, you need to set the client id here.
67-
68-
NSString *path = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info"
69-
ofType:@"plist"];
70-
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
71-
NSString *clientId = [dict objectForKey:@"CLIENT_ID"];
72-
73-
gsiHandler = [GoogleSignInHandler alloc];
74-
75-
// Setup the Sign-In instance.
76-
GIDSignIn *signIn = [GIDSignIn sharedInstance];
77-
signIn.clientID = clientId;
78-
signIn.uiDelegate = gsiHandler;
79-
signIn.delegate = gsiHandler;
80-
81-
// looks like it's just calling itself, but the implementations were swapped
82-
// so we're actually calling the original once we're done
8365
return [self GoogleSignInAppController:application
8466
didFinishLaunchingWithOptions:launchOptions];
8567
}
@@ -96,10 +78,7 @@ - (BOOL)GoogleSignInAppController:(UIApplication *)application
9678
sourceApplication:sourceApplication
9779
annotation:annotation];
9880

99-
return [[GIDSignIn sharedInstance] handleURL:url
100-
sourceApplication:sourceApplication
101-
annotation:annotation] ||
102-
handled;
81+
return [[GIDSignIn sharedInstance] handleURL:url] || handled;
10382
}
10483

10584
/**
@@ -109,16 +88,8 @@ - (BOOL)GoogleSignInAppController:(UIApplication *)app
10988
openURL:(NSURL *)url
11089
options:(NSDictionary *)options {
11190

112-
BOOL handled =
113-
[self GoogleSignInAppController:app openURL:url options:options];
114-
115-
return [[GIDSignIn sharedInstance]
116-
handleURL:url
117-
sourceApplication:
118-
options[UIApplicationOpenURLOptionsSourceApplicationKey]
119-
annotation:
120-
options[UIApplicationOpenURLOptionsAnnotationKey]] ||
121-
handled;
91+
BOOL handled = [self GoogleSignInAppController:app openURL:url options:options];
92+
return [[GIDSignIn sharedInstance] handleURL:url] || handled;
12293
}
12394

12495
@end

0 commit comments

Comments
 (0)