No Description

FIRBundleUtil.m 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "Private/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. // This allows app extensions that have the app's bundle as their prefix to pass this test.
  46. NSString *applicationBundleIdentifier =
  47. [GULAppEnvironmentUtil isAppExtension]
  48. ? [self bundleIdentifierByRemovingLastPartFrom:bundle.bundleIdentifier]
  49. : bundle.bundleIdentifier;
  50. if ([applicationBundleIdentifier isEqualToString:bundleIdentifier]) {
  51. return YES;
  52. }
  53. }
  54. return NO;
  55. }
  56. + (NSString *)bundleIdentifierByRemovingLastPartFrom:(NSString *)bundleIdentifier {
  57. NSString *bundleIDComponentsSeparator = @".";
  58. NSMutableArray<NSString *> *bundleIDComponents =
  59. [[bundleIdentifier componentsSeparatedByString:bundleIDComponentsSeparator] mutableCopy];
  60. [bundleIDComponents removeLastObject];
  61. return [bundleIDComponents componentsJoinedByString:bundleIDComponentsSeparator];
  62. }
  63. @end