FIRBundleUtil.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseCore/Sources/FIRBundleUtil.h"
  15. #if SWIFT_PACKAGE
  16. @import GoogleUtilities_Environment;
  17. #else
  18. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  19. #endif
  20. @implementation FIRBundleUtil
  21. + (NSArray *)relevantBundles {
  22. return @[ [NSBundle mainBundle], [NSBundle bundleForClass:[self class]] ];
  23. }
  24. + (NSString *)optionsDictionaryPathWithResourceName:(NSString *)resourceName
  25. andFileType:(NSString *)fileType
  26. inBundles:(NSArray *)bundles {
  27. // Loop through all bundles to find the config dict.
  28. for (NSBundle *bundle in bundles) {
  29. NSString *path = [bundle pathForResource:resourceName ofType:fileType];
  30. // Use the first one we find.
  31. if (path) {
  32. return path;
  33. }
  34. }
  35. return nil;
  36. }
  37. + (NSArray *)relevantURLSchemes {
  38. NSMutableArray *result = [[NSMutableArray alloc] init];
  39. for (NSBundle *bundle in [[self class] relevantBundles]) {
  40. NSArray *urlTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"];
  41. for (NSDictionary *urlType in urlTypes) {
  42. [result addObjectsFromArray:urlType[@"CFBundleURLSchemes"]];
  43. }
  44. }
  45. return result;
  46. }
  47. + (BOOL)hasBundleIdentifierPrefix:(NSString *)bundleIdentifier inBundles:(NSArray *)bundles {
  48. for (NSBundle *bundle in bundles) {
  49. if ([bundle.bundleIdentifier isEqualToString:bundleIdentifier]) {
  50. return YES;
  51. }
  52. if ([GULAppEnvironmentUtil isAppExtension]) {
  53. // A developer could be using the same `FIROptions` for both their app and extension. Since
  54. // extensions have a suffix added to the bundleID, we consider a matching prefix as valid.
  55. NSString *appBundleIDFromExtension =
  56. [self bundleIdentifierByRemovingLastPartFrom:bundle.bundleIdentifier];
  57. if ([appBundleIDFromExtension isEqualToString:bundleIdentifier]) {
  58. return YES;
  59. }
  60. }
  61. }
  62. return NO;
  63. }
  64. + (NSString *)bundleIdentifierByRemovingLastPartFrom:(NSString *)bundleIdentifier {
  65. NSString *bundleIDComponentsSeparator = @".";
  66. NSMutableArray<NSString *> *bundleIDComponents =
  67. [[bundleIdentifier componentsSeparatedByString:bundleIDComponentsSeparator] mutableCopy];
  68. [bundleIDComponents removeLastObject];
  69. return [bundleIDComponents componentsJoinedByString:bundleIDComponentsSeparator];
  70. }
  71. @end