Skip to content

Commit

Permalink
Merge pull request #959 from firebase/firebase-8
Browse files Browse the repository at this point in the history
Fix bundle resource loading for dynamic frameworks
  • Loading branch information
morganchen12 committed Jun 8, 2021
2 parents 8797fa7 + e9f4eae commit f0f172e
Show file tree
Hide file tree
Showing 44 changed files with 172 additions and 116 deletions.
3 changes: 1 addition & 2 deletions FirebaseAnonymousAuthUI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Pod::Spec.new do |s|
s.platform = :ios
s.ios.deployment_target = '10.0'
s.ios.framework = 'UIKit'
s.static_framework = true
s.requires_arc = true
s.cocoapods_version = '>= 1.8.0'
s.pod_target_xcconfig = {
Expand All @@ -21,7 +20,7 @@ Pod::Spec.new do |s|
s.dependency 'FirebaseAuthUI'
s.dependency 'FirebaseAuth', '~> 8.0'
s.dependency 'FirebaseCore'
s.resource_bundle = {
s.resource_bundles = {
'FirebaseAnonymousAuthUI' => [
'FirebaseAnonymousAuthUI/Sources/{Resources,Strings}/*.{png,lproj}'
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ @implementation FirebaseAnonymousAuthUITests

- (void)setUp {
[super setUp];

id mockUtilsClass = OCMClassMock([FUIAuthUtils class]);
OCMStub(ClassMethod([mockUtilsClass bundleNamed:OCMOCK_ANY])).
andReturn([NSBundle bundleForClass:[FUIAnonymousAuth class]]);

id authUIClass = OCMClassMock([FUIAuth class]);
OCMStub(ClassMethod([authUIClass authUIWithAuth:OCMOCK_ANY])).
Expand Down
11 changes: 9 additions & 2 deletions FirebaseAnonymousAuthUI/Sources/FUIAnonymousAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ @implementation FUIAnonymousAuth {
UIViewController *_presentingViewController;
}

+ (NSBundle *)bundle {
return [FUIAuthUtils bundleNamed:kBundleName
inFrameworkBundle:[NSBundle bundleForClass:[self class]]];
}

- (instancetype)init {
return [self initWithAuthUI:[FUIAuth defaultAuthUI]];
}
Expand Down Expand Up @@ -85,11 +90,13 @@ - (NSString *)shortName {
}

- (NSString *)signInLabel {
return FUILocalizedStringFromTableInBundle(kSignInAsGuest, kTableName, kBundleName);
return FUILocalizedStringFromTableInBundle(kSignInAsGuest,
kTableName,
[FUIAnonymousAuth bundle]);
}

- (UIImage *)icon {
return [FUIAuthUtils imageNamed:@"ic_anonymous" fromBundleNameOrNil:kBundleName];
return [FUIAuthUtils imageNamed:@"ic_anonymous" fromBundle:[FUIAnonymousAuth bundle]];
}

- (UIColor *)buttonBackgroundColor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property(nonatomic, readwrite) FUIButtonAlignment buttonAlignment;

/** @fn bundle
@brief Returns the FirebaseAnonymousAuthUI resource bundle.
*/
+ (NSBundle *)bundle;

/** @fn init
@brief Initialize the instance with the default AuthUI.
*/
Expand Down
2 changes: 1 addition & 1 deletion FirebaseAuthUI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Pod::Spec.new do |s|
s.dependency 'FirebaseAuth'
s.dependency 'FirebaseCore'
s.dependency 'GoogleUtilities/UserDefaults'
s.resource_bundle = {
s.resource_bundles = {
'FirebaseAuthUI' => ['FirebaseAuthUI/Sources/{Resources,Strings}/*.{xib,png,lproj}']
}

Expand Down
2 changes: 1 addition & 1 deletion FirebaseAuthUI/Sources/FUIAuthBaseViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil

- (instancetype)initWithAuthUI:(FUIAuth *)authUI {
return [self initWithNibName:NSStringFromClass([self class])
bundle:[FUIAuthUtils bundleNamed:FUIAuthBundleName]
bundle:[FUIAuthUtils authUIBundle]
authUI:authUI];
}

Expand Down
2 changes: 1 addition & 1 deletion FirebaseAuthUI/Sources/FUIAuthPickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ @implementation FUIAuthPickerViewController {

- (instancetype)initWithAuthUI:(FUIAuth *)authUI {
return [self initWithNibName:@"FUIAuthPickerViewController"
bundle:[FUIAuthUtils bundleNamed:FUIAuthBundleName]
bundle:[FUIAuthUtils authUIBundle]
authUI:authUI];
}

Expand Down
11 changes: 5 additions & 6 deletions FirebaseAuthUI/Sources/FUIAuthStrings.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@
}

NSString *FUILocalizedStringFromTable(NSString *key, NSString *table) {
return FUILocalizedStringFromTableInBundle(key, table, FUIAuthBundleName);
return FUILocalizedStringFromTableInBundle(key, table, [FUIAuthUtils authUIBundle]);
}

NSString *FUILocalizedStringFromTableInBundle(NSString *key,
NSString *table,
NSString *_Nullable bundleName) {
NSBundle *_Nullable bundle) {
// Don't load defaultAuthUI if the default app isn't configured. We don't recommend
// people do this in our docs, but if for whatever reason they want to use a custom
// app, this code shouldn't crash.
Expand All @@ -150,11 +150,10 @@
}
}
}
NSBundle *frameworkBundle = [FUIAuthUtils bundleNamed:bundleName];
if (frameworkBundle == nil) {
frameworkBundle = [NSBundle mainBundle];
if (bundle == nil) {
bundle = [NSBundle mainBundle];
}
return [frameworkBundle localizedStringForKey:key value:nil table:table];
return [bundle localizedStringForKey:key value:nil table:table];
}

NS_ASSUME_NONNULL_END
47 changes: 30 additions & 17 deletions FirebaseAuthUI/Sources/FUIAuthUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,53 @@

@implementation FUIAuthUtils

+ (nullable NSBundle *)bundleNamed:(nullable NSString *)bundleName {
NSBundle *frameworkBundle = nil;
+ (NSBundle *)authUIBundle {
return [self bundleNamed:FUIAuthBundleName
inFrameworkBundle:[NSBundle bundleForClass:[self class]]];
}

+ (nullable NSBundle *)bundleNamed:(nullable NSString *)bundleName
inFrameworkBundle:(nullable NSBundle *)framework {
NSBundle *returnBundle = nil;
if (!bundleName) {
bundleName = FUIAuthBundleName;
}
// Use the main bundle as a default if the framework wasn't provided.
NSBundle *frameworkBundle = framework;
if (frameworkBundle == nil) {
// If frameworkBundle is unspecified, assume main bundle/static linking.
frameworkBundle = [NSBundle mainBundle];
}
// If using static frameworks, the bundle will be included directly in the main
// bundle.
NSString *path = [[NSBundle mainBundle] pathForResource:bundleName ofType:@"bundle"];

// Otherwise, check the appropriate framework bundle.
if (!path) {
// Check framework resources if bundle isn't present in main bundle.
path = [[NSBundle mainBundle] pathForResource:bundleName ofType:@"framework"];
path = [frameworkBundle pathForResource:bundleName ofType:@"bundle"];
}
frameworkBundle = [NSBundle bundleWithPath:path];
if (!frameworkBundle) {
frameworkBundle = [NSBundle bundleForClass:[self class]];
if (!path) {
NSLog(@"Warning: Unable to find bundle %@ in framework %@.", bundleName, framework);
// Fall back on the root module.
return frameworkBundle;
}
return frameworkBundle;
returnBundle = [NSBundle bundleWithPath:path];
return returnBundle;
}

+ (nullable UIImage *)imageNamed:(NSString *)name fromBundle:(nullable NSBundle *)bundle {
if (!bundle) {
bundle = [self bundleNamed:nil];
bundle = [self authUIBundle];
}
NSString *path = [bundle pathForResource:name ofType:@"png"];
if (!path) {
NSLog(@"Warning: Unable to find asset %@ in bundle %@.", name, bundle);
}
return [UIImage imageWithContentsOfFile:path];
}

+ (nullable UIImage *)imageNamed:(NSString *)name fromBundleNameOrNil:(nullable NSString *)bundleNameOrNil {
NSString *path = [[FUIAuthUtils bundleNamed:bundleNameOrNil] pathForResource:name ofType:@"png"];
if (!path) {
NSLog(@"Warning: Unable to find asset %@ in bundle named %@.", name, bundleNameOrNil);
if (@available(iOS 13.0, *)) {
return [UIImage imageNamed:name inBundle:bundle withConfiguration:nil];
} else {
return [UIImage imageWithContentsOfFile:path];
}
return [UIImage imageWithContentsOfFile:path];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ - (instancetype)initWithContents:(nullable FUIStaticContentTableViewContent *)co
footerText:(nullable NSString *)footerText
footerAction:(nullable FUIStaticContentTableViewCellAction)footerAction {
if (self = [super initWithNibName:NSStringFromClass([self class])
bundle:[FUIAuthUtils bundleNamed:FUIAuthBundleName]
bundle:[FUIAuthUtils authUIBundle]
authUI:[FUIAuth defaultAuthUI]]) {
_tableViewManager.contents = contents;
_nextAction = [nextAction copy];
Expand Down
4 changes: 2 additions & 2 deletions FirebaseAuthUI/Sources/FUIStaticContentTableViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ - (void)setTableView:(nullable UITableView *)tableView {
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellReuseIdentitfier];

UINib *passwordCellNib = [UINib nibWithNibName:NSStringFromClass([FUIPasswordTableViewCell class])
bundle:[FUIAuthUtils bundleNamed:FUIAuthBundleName]];
bundle:[FUIAuthUtils authUIBundle]];
[tableView registerNib:passwordCellNib forCellReuseIdentifier:kPasswordCellReuseIdentitfier];

UINib *inputCellNib = [UINib nibWithNibName:NSStringFromClass([FUIInputTableViewCell class])
bundle:[FUIAuthUtils bundleNamed:FUIAuthBundleName]];
bundle:[FUIAuthUtils authUIBundle]];
[tableView registerNib:inputCellNib forCellReuseIdentifier:kInputCellReuseIdentitfier];
}

Expand Down
4 changes: 2 additions & 2 deletions FirebaseAuthUI/Sources/Public/FirebaseAuthUI/FUIAuthStrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ NSString *FUILocalizedStringFromTable(NSString *key, NSString *table);
@brief Gets a localized string from a name.
@param key The key value of the string.
@param table The localization table name.
@param bundleName The value of bundlu to look for. If nil is passed looking in apps bundle.
@param bundle The bundle containing the strings. If nil is provided, this function searches the main app bundle.
@return The string by the key localized in the current locale.
*/
NSString *FUILocalizedStringFromTableInBundle(NSString *key,
NSString *table,
NSString *_Nullable bundleName);
NSBundle *_Nullable bundle);

#ifdef __cplusplus
}
Expand Down
16 changes: 6 additions & 10 deletions FirebaseAuthUI/Sources/Public/FirebaseAuthUI/FUIAuthUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,22 @@ extern NSString *const FUIAuthBundleName;

- (instancetype)init NS_UNAVAILABLE;

+ (NSBundle *)authUIBundle;

/** @fn bundleNamed:
@brief Gets the framework bundle for specified name
@param bundleName Name of the bundle to retreive. If nil, this returns the default bundle for
FirebaseUI.
@param framework The name of the framework module the resource bundle should be present in.
*/
+ (nullable NSBundle *)bundleNamed:(nullable NSString *)bundleName;

/** @fn imageNamed:fromBundle:
@brief Gets a UIImage with the given name, assuming it's a png.
@param name Name of the image to retreive.
@param bundleNameOrNil Name of the bundle to retreive. If nil, this method will look into the
default FirebaseUI framework bundle.
*/
+ (nullable UIImage *)imageNamed:(NSString *)name fromBundleNameOrNil:(nullable NSString *)bundleNameOrNil;
+ (nullable NSBundle *)bundleNamed:(nullable NSString *)bundleName
inFrameworkBundle:(nullable NSBundle *)framework;

/** @fn imageNamed:fromBundle:
@brief Gets a UIImage with the given name, assuming it's a png.
@param name Name of the image to retreive.
@param bundle The bundle to retrieve the image from. If nil, this method will look into the
default FirebaseUI framework bundle.
default FirebaseAuthUI framework bundle.
*/
+ (nullable UIImage *)imageNamed:(NSString *)name fromBundle:(nullable NSBundle *)bundle;

Expand Down
2 changes: 1 addition & 1 deletion FirebaseEmailAuthUI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Pod::Spec.new do |s|
s.dependency 'FirebaseAuth', '~> 8.0'
s.dependency 'FirebaseCore'
s.dependency 'GoogleUtilities/UserDefaults'
s.resource_bundle = {
s.resource_bundles = {
'FirebaseEmailAuthUI' => ['FirebaseEmailAuthUI/Sources/Resources/*.{xib,png}']
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ @implementation FirebaseEmailAuthUITests

- (void)setUp {
[super setUp];

id mockUtilsClass = OCMClassMock([FUIAuthUtils class]);
OCMStub(ClassMethod([mockUtilsClass bundleNamed:OCMOCK_ANY])).
andReturn([NSBundle bundleForClass:[FUIEmailAuth class]]);

id authUIClass = OCMClassMock([FUIAuth class]);
OCMStub([authUIClass setEmailAuthProvider:[OCMArg any]]).andDo(^(NSInvocation *invocation){
Expand Down
4 changes: 2 additions & 2 deletions FirebaseEmailAuthUI/Sources/FUIConfirmEmailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ @implementation FUIConfirmEmailViewController

- (instancetype)initWithAuthUI:(FUIAuth *)authUI {
return [self initWithNibName:NSStringFromClass([self class])
bundle:[FUIAuthUtils bundleNamed:FUIEmailAuthBundleName]
bundle:[FUIEmailAuth bundle]
authUI:authUI];
}

Expand Down Expand Up @@ -207,7 +207,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView
FUIAuthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentifier];
if (!cell) {
UINib *cellNib = [UINib nibWithNibName:NSStringFromClass([FUIAuthTableViewCell class])
bundle:[FUIAuthUtils bundleNamed:FUIAuthBundleName]];
bundle:[FUIAuthUtils authUIBundle]];
[tableView registerNib:cellNib forCellReuseIdentifier:kCellReuseIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentifier];
}
Expand Down
7 changes: 6 additions & 1 deletion FirebaseEmailAuthUI/Sources/FUIEmailAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ @interface FUIEmailAuth () <FUIEmailAuthProvider>

@implementation FUIEmailAuth

+ (NSBundle *)bundle {
return [FUIAuthUtils bundleNamed:FUIEmailAuthBundleName
inFrameworkBundle:[NSBundle bundleForClass:[self class]]];
}

- (instancetype)init {
return [self initAuthAuthUI:[FUIAuth defaultAuthUI]
signInMethod:FIREmailPasswordAuthSignInMethod
Expand Down Expand Up @@ -143,7 +148,7 @@ - (NSString *)signInLabel {
}

- (UIImage *)icon {
return [FUIAuthUtils imageNamed:@"ic_email" fromBundleNameOrNil:FUIEmailAuthBundleName];
return [FUIAuthUtils imageNamed:@"ic_email" fromBundle:[FUIEmailAuth bundle]];
}

- (UIColor *)buttonBackgroundColor {
Expand Down
3 changes: 2 additions & 1 deletion FirebaseEmailAuthUI/Sources/FUIEmailAuthStrings.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
//

#import "FirebaseEmailAuthUI/Sources/Public/FirebaseEmailAuthUI/FUIEmailAuth.h"
#import "FirebaseEmailAuthUI/Sources/FUIEmailAuthStrings.h"

#if SWIFT_PACKAGE
Expand All @@ -30,5 +31,5 @@
NSString *FUIEmailAuthLocalizedString(NSString *key) {
return FUILocalizedStringFromTableInBundle(key,
kEmailAuthProviderTableName,
FUIEmailAuthBundleName);
[FUIEmailAuth bundle]);
}
4 changes: 2 additions & 2 deletions FirebaseEmailAuthUI/Sources/FUIEmailEntryViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ @implementation FUIEmailEntryViewController {

- (instancetype)initWithAuthUI:(FUIAuth *)authUI {
return [self initWithNibName:NSStringFromClass([self class])
bundle:[FUIAuthUtils bundleNamed:FUIEmailAuthBundleName]
bundle:[FUIEmailAuth bundle]
authUI:authUI];
}

Expand Down Expand Up @@ -280,7 +280,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView
FUIAuthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentifier];
if (!cell) {
UINib *cellNib = [UINib nibWithNibName:NSStringFromClass([FUIAuthTableViewCell class])
bundle:[FUIAuthUtils bundleNamed:FUIAuthBundleName]];
bundle:[FUIAuthUtils authUIBundle]];
[tableView registerNib:cellNib forCellReuseIdentifier:kCellReuseIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentifier];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import <FirebaseAuth/FirebaseAuth.h>
#import <FirebaseAuthUI/FirebaseAuthUI.h>

#import "FirebaseEmailAuthUI/Sources/Public/FirebaseEmailAuthUI/FUIEmailAuth.h"
#import "FirebaseEmailAuthUI/Sources/FUIEmailAuthStrings.h"

/** @var kCellReuseIdentifier
Expand Down Expand Up @@ -60,7 +61,7 @@ @implementation FUIPasswordRecoveryViewController {
- (instancetype)initWithAuthUI:(FUIAuth *)authUI
email:(NSString *_Nullable)email {
return [self initWithNibName:NSStringFromClass([self class])
bundle:[FUIAuthUtils bundleNamed:FUIEmailAuthBundleName]
bundle:[FUIEmailAuth bundle]
authUI:authUI
email:email];
}
Expand Down Expand Up @@ -168,7 +169,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView
FUIAuthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentifier];
if (!cell) {
UINib *cellNib = [UINib nibWithNibName:NSStringFromClass([FUIAuthTableViewCell class])
bundle:[FUIAuthUtils bundleNamed:FUIAuthBundleName]];
bundle:[FUIAuthUtils authUIBundle]];
[tableView registerNib:cellNib forCellReuseIdentifier:kCellReuseIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentifier];
}
Expand Down

0 comments on commit f0f172e

Please sign in to comment.