22
22
#import < FirebaseCore/FIRApp.h>
23
23
#import < FirebaseCore/FIROptions.h>
24
24
#import < FirebaseAuth/FIRGoogleAuthProvider.h>
25
- #import " ApplicationDelegate.h"
26
-
27
- /* * @typedef GoogleSignInCallback
28
- @brief The type of block invoked when a @c GIDGoogleUser object is ready or an error has
29
- occurred.
30
- @param user The Google user if any.
31
- @param error The error which occurred, if any.
32
- */
33
- typedef void (^GoogleSignInCallback)(GIDGoogleUser *user, NSError *error);
34
-
35
- /* * @class GoogleAuthDelegate
36
- @brief The designated delegate class for Google Sign-In.
37
- */
38
- @interface GoogleAuthDelegate : NSObject <GIDSignInDelegate, OpenURLDelegate>
39
-
40
- /* * @fn initWithPresentingViewController:callback:
41
- @brief Initializes the new instance with the callback.
42
- @param presentingViewController The view controller to present the UI.
43
- @param callback A block which is invoked when the sign-in flow finishes. Invoked asynchronously
44
- on an unspecified thread in the future.
45
- */
46
- - (instancetype )initWithPresentingViewController : (UIViewController *)presentingViewController
47
- callback : (nullable GoogleSignInCallback)callback ;
48
-
49
- @end
50
-
51
- @implementation GoogleAuthDelegate {
52
- UIViewController *_presentingViewController;
53
- GoogleSignInCallback _callback;
54
- }
55
-
56
- - (instancetype )initWithPresentingViewController : (UIViewController *)presentingViewController
57
- callback : (nullable GoogleSignInCallback)callback {
58
- self = [super init ];
59
- if (self) {
60
- _presentingViewController = presentingViewController;
61
- _callback = callback;
62
- }
63
- return self;
64
- }
65
-
66
- - (void )signIn : (GIDSignIn *)signIn
67
- didSignInForUser : (GIDGoogleUser *)user
68
- withError : (NSError *)error {
69
- GoogleSignInCallback callback = _callback;
70
- _callback = nil ;
71
- if (callback) {
72
- callback (user, error);
73
- }
74
- }
75
-
76
- - (BOOL )handleOpenURL : (NSURL *)url sourceApplication : (NSString *)sourceApplication {
77
- return [[GIDSignIn sharedInstance ] handleURL: url];
78
- }
79
-
80
- @end
81
25
82
26
@implementation GoogleAuthProvider
83
27
84
28
- (void )getAuthCredentialWithPresentingViewController : (UIViewController *)viewController
85
29
callback : (AuthCredentialCallback)callback {
86
30
[self signOut ];
87
31
88
- // The delegate needs to be retained.
89
- __block GoogleAuthDelegate *delegate = [[GoogleAuthDelegate alloc ]
90
- initWithPresentingViewController: viewController
91
- callback: ^(GIDGoogleUser *user, NSError *error) {
92
- [ApplicationDelegate setOpenURLDelegate: nil ];
93
- delegate = nil ;
32
+ GIDSignIn *signIn = GIDSignIn.sharedInstance ;
33
+ GIDConfiguration *config = [[GIDConfiguration alloc ] initWithClientID: [self googleClientID ]];
34
+ [signIn signInWithConfiguration: config
35
+ presentingViewController: viewController
36
+ callback: ^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) {
94
37
if (error) {
95
38
callback (nil , error);
96
39
return ;
@@ -100,17 +43,10 @@ - (void)getAuthCredentialWithPresentingViewController:(UIViewController *)viewCo
100
43
accessToken: auth.accessToken];
101
44
callback (credential, error);
102
45
}];
103
- GIDSignIn *signIn = [GIDSignIn sharedInstance ];
104
- signIn.clientID = [self googleClientID ];
105
- signIn.shouldFetchBasicProfile = YES ;
106
- signIn.delegate = delegate;
107
- signIn.presentingViewController = viewController;
108
- [ApplicationDelegate setOpenURLDelegate: delegate];
109
- [signIn signIn ];
110
46
}
111
47
112
48
- (void )signOut {
113
- [[ GIDSignIn sharedInstance ] signOut ];
49
+ [GIDSignIn. sharedInstance signOut ];
114
50
}
115
51
116
52
- (NSString *)googleClientID {
0 commit comments