FIRBundleUtil.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  16. @implementation FIRBundleUtil
  17. + (NSArray *)relevantBundles {
  18. return @[ [NSBundle mainBundle], [NSBundle bundleForClass:[self class]] ];
  19. }
  20. + (NSString *)optionsDictionaryPathWithResourceName:(NSString *)resourceName
  21. andFileType:(NSString *)fileType
  22. inBundles:(NSArray *)bundles {
  23. // Loop through all bundles to find the config dict.
  24. for (NSBundle *bundle in bundles) {
  25. NSString *path = [bundle pathForResource:resourceName ofType:fileType];
  26. // Use the first one we find.
  27. if (path) {
  28. return path;
  29. }
  30. }
  31. return nil;
  32. }
  33. + (NSArray *)relevantURLSchemes {
  34. NSMutableArray *result = [[NSMutableArray alloc] init];
  35. for (NSBundle *bundle in [[self class] relevantBundles]) {
  36. NSArray *urlTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"];
  37. for (NSDictionary *urlType in urlTypes) {
  38. [result addObjectsFromArray:urlType[@"CFBundleURLSchemes"]];
  39. }
  40. }
  41. return result;
  42. }
  43. + (BOOL)hasBundleIdentifierPrefix:(NSString *)bundleIdentifier inBundles:(NSArray *)bundles {
  44. for (NSBundle *bundle in bundles) {
  45. if ([bundle.bundleIdentifier isEqualToString:bundleIdentifier]) {
  46. return YES;
  47. }
  48. if ([GULAppEnvironmentUtil isAppExtension]) {
  49. // A developer could be using the same `FIROptions` for both their app and extension. Since
  50. // extensions have a suffix added to the bundleID, we consider a matching prefix as valid.
  51. NSString *appBundleIDFromExtension =
  52. [self bundleIdentifierByRemovingLastPartFrom:bundle.bundleIdentifier];
  53. if ([appBundleIDFromExtension isEqualToString:bundleIdentifier]) {
  54. return YES;
  55. }
  56. }
  57. }
  58. return NO;
  59. }
  60. + (NSString *)bundleIdentifierByRemovingLastPartFrom:(NSString *)bundleIdentifier {
  61. NSString *bundleIDComponentsSeparator = @".";
  62. NSMutableArray<NSString *> *bundleIDComponents =
  63. [[bundleIdentifier componentsSeparatedByString:bundleIDComponentsSeparator] mutableCopy];
  64. [bundleIDComponents removeLastObject];
  65. return [bundleIDComponents componentsJoinedByString:bundleIDComponentsSeparator];
  66. }
  67. @end