16
16
17
17
#import " MainViewController+OAuth.h"
18
18
19
+ #import < AuthenticationServices/AuthenticationServices.h>
20
+
19
21
#import " AppManager.h"
20
22
#import " FIROAuthProvider.h"
21
23
#import " MainViewController+Internal.h"
22
24
23
25
NS_ASSUME_NONNULL_BEGIN
24
26
27
+ @interface MainViewController () <ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>
28
+
29
+ @end
30
+
25
31
@implementation MainViewController (OAuth)
26
32
27
33
- (StaticContentTableViewSection *)oAuthSection {
@@ -33,6 +39,14 @@ - (StaticContentTableViewSection *)oAuthSection {
33
39
action: ^{ [weakSelf linkWithGoogleHeadfulLite ]; }],
34
40
[StaticContentTableViewCell cellWithTitle: @" Reauthenticate with Google"
35
41
action: ^{ [weakSelf reauthenticateWithGoogleHeadfulLite ]; }],
42
+ [StaticContentTableViewCell cellWithTitle: @" Sign in with Apple"
43
+ action: ^{ [weakSelf signInWithApple ]; }],
44
+ [StaticContentTableViewCell cellWithTitle: @" Link with Apple"
45
+ action: ^{ [weakSelf linkWithApple ]; }],
46
+ [StaticContentTableViewCell cellWithTitle: @" Unlink with Apple"
47
+ action: ^{ [weakSelf unlinkFromProvider: @" apple.com" completion: nil ]; }],
48
+ [StaticContentTableViewCell cellWithTitle: @" Reauthenticate with Apple"
49
+ action: ^{ [weakSelf reauthenticateWithApple ]; }],
36
50
[StaticContentTableViewCell cellWithTitle: @" Sign in with Twitter"
37
51
action: ^{ [weakSelf signInTwitterHeadfulLite ]; }],
38
52
[StaticContentTableViewCell cellWithTitle: @" Sign in with GitHub"
@@ -245,7 +259,6 @@ - (void)signInLinkedinHeadfulLite {
245
259
}];
246
260
}
247
261
248
-
249
262
- (void )signInMicrosoftHeadfulLite {
250
263
FIROAuthProvider *provider = self.microsoftOAuthProvider ;
251
264
provider.customParameters = @{
@@ -305,6 +318,86 @@ - (void)signInYahooHeadfulLite {
305
318
}];
306
319
}
307
320
321
+ - (ASAuthorizationAppleIDRequest *)appleIDRequestWithState : (NSString *)state API_AVAILABLE(ios(13.0 )) {
322
+ ASAuthorizationAppleIDRequest *request = [[[ASAuthorizationAppleIDProvider alloc ] init ] createRequest ];
323
+ request.requestedScopes = @[ASAuthorizationScopeEmail, ASAuthorizationScopeFullName];
324
+ request.nonce = @" REPLACE_ME_WITH_YOUR_NONCE" ;
325
+ request.state = state;
326
+ return request;
327
+ }
328
+
329
+ - (void )signInWithApple {
330
+ if (@available (iOS 13 , *)) {
331
+ ASAuthorizationAppleIDRequest* request = [self appleIDRequestWithState: @" signIn" ];
332
+
333
+ ASAuthorizationController* controller = [[ASAuthorizationController alloc ] initWithAuthorizationRequests: @[request]];
334
+ controller.delegate = self;
335
+ controller.presentationContextProvider = self;
336
+ [controller performRequests ];
337
+ }
338
+ }
339
+
340
+ - (void )linkWithApple {
341
+ if (@available (iOS 13 , *)) {
342
+ ASAuthorizationAppleIDRequest* request = [self appleIDRequestWithState: @" link" ];
343
+
344
+ ASAuthorizationController* controller = [[ASAuthorizationController alloc ] initWithAuthorizationRequests: @[request]];
345
+ controller.delegate = self;
346
+ controller.presentationContextProvider = self;
347
+ [controller performRequests ];
348
+ }
349
+ }
350
+
351
+ - (void )reauthenticateWithApple {
352
+ if (@available (iOS 13 , *)) {
353
+ ASAuthorizationAppleIDRequest* request = [self appleIDRequestWithState: @" reauth" ];
354
+
355
+ ASAuthorizationController* controller = [[ASAuthorizationController alloc ] initWithAuthorizationRequests: @[request]];
356
+ controller.delegate = self;
357
+ controller.presentationContextProvider = self;
358
+ [controller performRequests ];
359
+ }
360
+ }
361
+
362
+ - (void )authorizationController : (ASAuthorizationController *)controller didCompleteWithAuthorization : (ASAuthorization *)authorization API_AVAILABLE(ios(13.0 )) {
363
+ ASAuthorizationAppleIDCredential* appleIDCredential = authorization.credential ;
364
+ NSString *idToken = [NSString stringWithUTF8String: [appleIDCredential.identityToken bytes ]];
365
+ FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID: @" apple.com"
366
+ IDToken: idToken
367
+ rawNonce: @" REPLACE_ME_WITH_YOUR_RAW_NONCE"
368
+ accessToken: nil ];
369
+
370
+ if ([appleIDCredential.state isEqualToString: @" signIn" ]) {
371
+ [FIRAuth.auth signInWithCredential: credential completion: ^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
372
+ if (!error) {
373
+ NSLog (@" %@ " , authResult.description );
374
+ } else {
375
+ NSLog (@" %@ " , error.description );
376
+ }
377
+ }];
378
+ } else if ([appleIDCredential.state isEqualToString: @" link" ]) {
379
+ [FIRAuth.auth.currentUser linkWithCredential: credential completion: ^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
380
+ if (!error) {
381
+ NSLog (@" %@ " , authResult.description );
382
+ } else {
383
+ NSLog (@" %@ " , error.description );
384
+ }
385
+ }];
386
+ } else if ([appleIDCredential.state isEqualToString: @" reauth" ]) {
387
+ [FIRAuth.auth.currentUser reauthenticateWithCredential: credential completion: ^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
388
+ if (!error) {
389
+ NSLog (@" %@ " , authResult.description );
390
+ } else {
391
+ NSLog (@" %@ " , error.description );
392
+ }
393
+ }];
394
+ }
395
+ }
396
+
397
+ - (void )authorizationController : (ASAuthorizationController *)controller didCompleteWithError : (NSError *)error API_AVAILABLE(ios(13.0 )) {
398
+ NSLog (@" %@ " , error.description );
399
+ }
400
+
308
401
@end
309
402
310
403
NS_ASSUME_NONNULL_END
0 commit comments