|
| 1 | +// |
| 2 | +// Copyright (c) 2016 Google Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#import <FirebaseAuth/FIRTwitterAuthProvider.h> |
| 18 | +#import <FirebaseAuthUI/FIRAuthUIErrorUtils.h> |
| 19 | +#import <TwitterKit/TwitterKit.h> |
| 20 | +#import "FIRTwitterAuthUI.h" |
| 21 | + |
| 22 | +/** @var kBundleFileName |
| 23 | + @brief The name of the bundle containing Facebook auth provider assets/resources. |
| 24 | + */ |
| 25 | +static NSString *const kBundleFileName = @"FirebaseTwitterAuthUIBundle.bundle"; |
| 26 | + |
| 27 | +/** @var kTableName |
| 28 | + @brief The name of the strings table to search for localized strings. |
| 29 | + */ |
| 30 | +static NSString *const kTableName = @"FirebaseTwitterAuthUI"; |
| 31 | + |
| 32 | +/** @var kSignInWithFacebook |
| 33 | + @brief The string key for localized button text. |
| 34 | + */ |
| 35 | +static NSString *const kSignInWithTwitter = @"SignInWithTwitter"; |
| 36 | + |
| 37 | +@implementation FIRTwitterAuthUI |
| 38 | + |
| 39 | +/** @fn frameworkBundle |
| 40 | + @brief Returns the auth provider's resource bundle. |
| 41 | + @return Resource bundle for the auth provider. |
| 42 | + */ |
| 43 | ++ (NSBundle *)frameworkBundle { |
| 44 | + static NSBundle *frameworkBundle = nil; |
| 45 | + static dispatch_once_t predicate; |
| 46 | + dispatch_once(&predicate, ^{ |
| 47 | + NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath]; |
| 48 | + NSString *frameworkBundlePath = |
| 49 | + [mainBundlePath stringByAppendingPathComponent:kBundleFileName]; |
| 50 | + frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath]; |
| 51 | + if (!frameworkBundle) { |
| 52 | + frameworkBundle = [NSBundle mainBundle]; |
| 53 | + } |
| 54 | + }); |
| 55 | + return frameworkBundle; |
| 56 | +} |
| 57 | + |
| 58 | +/** @fn imageNamed: |
| 59 | + @brief Returns an image from the resource bundle given a resource name. |
| 60 | + @param name The name of the image file. |
| 61 | + @return The image object for the named file. |
| 62 | + */ |
| 63 | ++ (UIImage *)imageNamed:(NSString *)name { |
| 64 | + NSString *path = [[[self class] frameworkBundle] pathForResource:name ofType:@"png"]; |
| 65 | + return [UIImage imageWithContentsOfFile:path]; |
| 66 | +} |
| 67 | + |
| 68 | +/** @fn localizedStringForKey: |
| 69 | + @brief Returns the localized text associated with a given string key. Will default to english |
| 70 | + text if the string is not available for the current localization. |
| 71 | + @param key A string key which identifies localized text in the .strings files. |
| 72 | + @return Localized value of the string identified by the key. |
| 73 | + */ |
| 74 | ++ (NSString *)localizedStringForKey:(NSString *)key { |
| 75 | + NSBundle *frameworkBundle = [[self class] frameworkBundle]; |
| 76 | + return [frameworkBundle localizedStringForKey:key value:nil table:kTableName]; |
| 77 | +} |
| 78 | + |
| 79 | +#pragma mark - FIRAuthProviderUI |
| 80 | + |
| 81 | +- (NSString *)providerID { |
| 82 | + return FIRTwitterAuthProviderID; |
| 83 | +} |
| 84 | + |
| 85 | +- (NSString *)shortName { |
| 86 | + return @"Twitter"; |
| 87 | +} |
| 88 | + |
| 89 | +- (NSString *)signInLabel { |
| 90 | + return [[self class] localizedStringForKey:kSignInWithTwitter]; |
| 91 | +} |
| 92 | + |
| 93 | +- (UIImage *)icon { |
| 94 | + return [[self class] imageNamed:@"ic_twitter"]; |
| 95 | +} |
| 96 | + |
| 97 | +- (UIColor *)buttonBackgroundColor { |
| 98 | + return [UIColor colorWithRed:71.0f/255.0f green:154.0f/255.0f blue:234.0f/255.0f alpha:1.0f]; |
| 99 | +} |
| 100 | + |
| 101 | +- (UIColor *)buttonTextColor { |
| 102 | + return [UIColor whiteColor]; |
| 103 | +} |
| 104 | + |
| 105 | +- (void)signInWithAuth:(FIRAuth *)auth |
| 106 | + email:(nullable NSString *)email |
| 107 | +presentingViewController:(nullable UIViewController *)presentingViewController |
| 108 | + completion:(nullable FIRAuthProviderSignInCompletionBlock)completion { |
| 109 | + |
| 110 | + [[Twitter sharedInstance] |
| 111 | + logInWithCompletion:^(TWTRSession * _Nullable session, NSError * _Nullable error) { |
| 112 | + if (session) { |
| 113 | + FIRAuthCredential *credential = |
| 114 | + [FIRTwitterAuthProvider credentialWithToken:session.authToken |
| 115 | + secret:session.authTokenSecret]; |
| 116 | + if (completion) { |
| 117 | + completion(credential, error); |
| 118 | + } |
| 119 | + } else { |
| 120 | + NSError *newError = |
| 121 | + [FIRAuthUIErrorUtils providerErrorWithUnderlyingError:error |
| 122 | + providerID:FIRTwitterAuthProviderID]; |
| 123 | + if (completion) { |
| 124 | + completion(nil, error); |
| 125 | + } |
| 126 | + } |
| 127 | + }]; |
| 128 | +} |
| 129 | + |
| 130 | +- (void)signOutWithAuth:(FIRAuth *)auth { |
| 131 | + NSString *twitterUserID = [TWTRAPIClient clientWithCurrentUser].userID; |
| 132 | + if (twitterUserID) { |
| 133 | + [[Twitter sharedInstance].sessionStore logOutUserID:twitterUserID]; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +- (BOOL)handleOpenURL:(NSURL *)URL sourceApplication:(NSString *)sourceApplication { |
| 138 | + return [[Twitter sharedInstance] application:[UIApplication sharedApplication] |
| 139 | + openURL:URL options:nil]; |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +@end |
0 commit comments