Skip to content

Commit b378456

Browse files
authored
Add apple sign in to sample code (#4583)
1 parent 979c185 commit b378456

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

Example/Auth/Sample/MainViewController+OAuth.m

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616

1717
#import "MainViewController+OAuth.h"
1818

19+
#import <AuthenticationServices/AuthenticationServices.h>
20+
1921
#import "AppManager.h"
2022
#import "FIROAuthProvider.h"
2123
#import "MainViewController+Internal.h"
2224

2325
NS_ASSUME_NONNULL_BEGIN
2426

27+
@interface MainViewController () <ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>
28+
29+
@end
30+
2531
@implementation MainViewController (OAuth)
2632

2733
- (StaticContentTableViewSection *)oAuthSection {
@@ -33,6 +39,14 @@ - (StaticContentTableViewSection *)oAuthSection {
3339
action:^{ [weakSelf linkWithGoogleHeadfulLite]; }],
3440
[StaticContentTableViewCell cellWithTitle:@"Reauthenticate with Google"
3541
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]; }],
3650
[StaticContentTableViewCell cellWithTitle:@"Sign in with Twitter"
3751
action:^{ [weakSelf signInTwitterHeadfulLite]; }],
3852
[StaticContentTableViewCell cellWithTitle:@"Sign in with GitHub"
@@ -245,7 +259,6 @@ - (void)signInLinkedinHeadfulLite {
245259
}];
246260
}
247261

248-
249262
- (void)signInMicrosoftHeadfulLite {
250263
FIROAuthProvider *provider = self.microsoftOAuthProvider;
251264
provider.customParameters = @{
@@ -305,6 +318,86 @@ - (void)signInYahooHeadfulLite {
305318
}];
306319
}
307320

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+
308401
@end
309402

310403
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)