|
25 | 25 | // Uncomment when using frawemorks
|
26 | 26 | //@import FirebaseGoogleAuthUI;
|
27 | 27 | #import <FIRGoogleAuthUI.h>
|
| 28 | +// Uncomment when using frawemorks |
| 29 | +//@import FirebaseFacebookAuthUI; |
| 30 | +#import <FIRFacebookAuthUI.h> |
28 | 31 |
|
29 | 32 | @interface FIRAuthViewController ()
|
30 | 33 | @property (weak, nonatomic) IBOutlet UITableViewCell *cellSignIn;
|
@@ -54,16 +57,27 @@ - (void)viewWillAppear:(BOOL)animated {
|
54 | 57 | [super viewWillAppear:animated];
|
55 | 58 |
|
56 | 59 | NSString *googleId = [[FIRApp defaultApp] options].clientID;
|
57 |
| - NSArray<id<FIRAuthProviderUI>> *providers = [NSArray arrayWithObjects:[[FIRGoogleAuthUI alloc] initWithClientID:googleId] , nil]; |
| 60 | + NSString *facebookAppId = [self readFacebookAppId]; |
| 61 | + |
| 62 | + NSArray<id<FIRAuthProviderUI>> *providers = [NSArray arrayWithObjects: |
| 63 | + [[FIRGoogleAuthUI alloc] initWithClientID:googleId], |
| 64 | + [[FIRFacebookAuthUI alloc] initWithAppID:facebookAppId], |
| 65 | + nil]; |
58 | 66 | _authUI.providers = providers;
|
59 | 67 |
|
| 68 | + __weak FIRAuthViewController *weakSelf = self; |
60 | 69 | self.authStateDidChangeHandle = [self.auth addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
|
61 |
| - [self updateUI:auth withUser:user]; |
| 70 | + [weakSelf updateUI:auth withUser:user]; |
62 | 71 | }];
|
63 | 72 |
|
64 | 73 |
|
65 | 74 | }
|
66 | 75 |
|
| 76 | +-(void)viewDidDisappear:(BOOL)animated { |
| 77 | + [super viewDidDisappear:animated]; |
| 78 | + [self.auth removeAuthStateDidChangeListener:self.authStateDidChangeHandle]; |
| 79 | +} |
| 80 | + |
67 | 81 | - (void)updateUI:(FIRAuth * _Nonnull) auth withUser:(FIRUser * _Nullable) user {
|
68 | 82 | self.authUser = user;
|
69 | 83 | if (user) {
|
@@ -97,5 +111,27 @@ - (IBAction)onAuthorization:(id)sender {
|
97 | 111 | }
|
98 | 112 | }
|
99 | 113 |
|
| 114 | +#pragma mark - Helper Methods |
| 115 | + |
| 116 | +// Helper method to retrieve FB app ID from info.plist |
| 117 | +- (NSString *)readFacebookAppId { |
| 118 | + NSString *facebookAppId = nil; |
| 119 | + NSArray *urlTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"]; |
| 120 | + for (NSDictionary *type in urlTypes) { |
| 121 | + if ([(NSString *)type[@"CFBundleURLName"] isEqualToString:@"FACEBOOK_APP_ID"]) { |
| 122 | + NSArray *urlSchemes = type[@"CFBundleURLSchemes"]; |
| 123 | + if (urlSchemes.count == 1) { |
| 124 | + facebookAppId = urlSchemes.firstObject; |
| 125 | + if (facebookAppId.length > 2) { |
| 126 | + facebookAppId = [facebookAppId substringFromIndex:2]; |
| 127 | + } |
| 128 | + } |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return facebookAppId; |
| 134 | +} |
| 135 | + |
100 | 136 |
|
101 | 137 | @end
|
0 commit comments