FIROptions.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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/Extension/FIRAppInternal.h"
  15. #import "FirebaseCore/Extension/FIRLogger.h"
  16. #import "FirebaseCore/Extension/FIROptionsInternal.h"
  17. #import "FirebaseCore/Sources/FIRBundleUtil.h"
  18. #import "FirebaseCore/Sources/Public/FirebaseCore/FIRVersion.h"
  19. // Keys for the strings in the plist file.
  20. NSString *const kFIRAPIKey = @"API_KEY";
  21. NSString *const kFIRTrackingID = @"TRACKING_ID";
  22. NSString *const kFIRGoogleAppID = @"GOOGLE_APP_ID";
  23. NSString *const kFIRClientID = @"CLIENT_ID";
  24. NSString *const kFIRGCMSenderID = @"GCM_SENDER_ID";
  25. NSString *const kFIRAndroidClientID = @"ANDROID_CLIENT_ID";
  26. NSString *const kFIRDatabaseURL = @"DATABASE_URL";
  27. NSString *const kFIRStorageBucket = @"STORAGE_BUCKET";
  28. // The key to locate the expected bundle identifier in the plist file.
  29. NSString *const kFIRBundleID = @"BUNDLE_ID";
  30. // The key to locate the project identifier in the plist file.
  31. NSString *const kFIRProjectID = @"PROJECT_ID";
  32. NSString *const kFIRIsMeasurementEnabled = @"IS_MEASUREMENT_ENABLED";
  33. NSString *const kFIRIsAnalyticsCollectionEnabled = @"FIREBASE_ANALYTICS_COLLECTION_ENABLED";
  34. NSString *const kFIRIsAnalyticsCollectionDeactivated = @"FIREBASE_ANALYTICS_COLLECTION_DEACTIVATED";
  35. // Keys for the JSON config file
  36. NSString *const kFIRJSONConfigVersion = @"version";
  37. NSString *const kFIRJSONConfigProjectNumber = @"project_number";
  38. NSString *const kFIRJSONConfigProjectID = @"project_id";
  39. NSString *const kFIRJSONConfigAppID = @"app_id";
  40. NSString *const kFIRJSONConfigBundleID = @"bundle_id";
  41. NSString *const kFIRJSONConfigApiKey = @"api_key";
  42. NSString *const kFIRJSONConfigRTDBURL = @"realtime_database_url";
  43. NSString *const kFIRJSONConfigStorageBucket = @"storage_bucket";
  44. NSString *const kFIRJSONConfigClientID = @"oauth_client_id";
  45. // Library version ID formatted like:
  46. // @"5" // Major version (one or more digits)
  47. // @"04" // Minor version (exactly 2 digits)
  48. // @"01" // Build number (exactly 2 digits)
  49. // @"000"; // Fixed "000"
  50. NSString *kFIRLibraryVersionID;
  51. // Plist file name.
  52. NSString *const kServiceInfoFileName = @"GoogleService-Info";
  53. // Plist file type.
  54. NSString *const kServiceInfoFileType = @"plist";
  55. // json file name.
  56. NSString *const kJsonFileName = @"firebase-sdk-config-apple";
  57. // json file type.
  58. NSString *const kJsonFileType = @"json";
  59. // Exception raised from attempting to modify a FIROptions after it's been copied to a FIRApp.
  60. NSString *const kFIRExceptionBadModification =
  61. @"Attempted to modify options after it's set on FIRApp. Please modify all properties before "
  62. @"initializing FIRApp.";
  63. @interface FIROptions ()
  64. /**
  65. * This property maintains the actual configuration key-value pairs.
  66. */
  67. @property(nonatomic, readwrite) NSMutableDictionary *optionsDictionary;
  68. /**
  69. * Calls `analyticsOptionsDictionaryWithInfoDictionary:` using [NSBundle mainBundle].infoDictionary.
  70. * It combines analytics options from both the infoDictionary and the config file.
  71. * Values which are present in the main plist override values from the GoogleService-Info.plist.
  72. */
  73. @property(nonatomic, readonly) NSDictionary *analyticsOptionsDictionary;
  74. /**
  75. * Version of the config file.
  76. */
  77. @property(nonatomic, readonly) NSInteger version;
  78. /**
  79. * Combination of analytics options from both the infoDictionary and the GoogleService-Info.plist.
  80. * Values which are present in the infoDictionary override values from the GoogleService-Info.plist.
  81. */
  82. - (NSDictionary *)analyticsOptionsDictionaryWithInfoDictionary:(NSDictionary *)infoDictionary;
  83. /**
  84. * Throw exception if editing is locked when attempting to modify an option.
  85. */
  86. - (void)checkEditingLocked;
  87. @end
  88. @implementation FIROptions {
  89. /// Backing variable for self.analyticsOptionsDictionary.
  90. NSDictionary *_analyticsOptionsDictionary;
  91. }
  92. static FIROptions *sDefaultOptions = nil;
  93. static NSDictionary *sDefaultOptionsDictionary = nil;
  94. static dispatch_once_t sDefaultOptionsOnceToken;
  95. static dispatch_once_t sDefaultOptionsDictionaryOnceToken;
  96. #pragma mark - Public only for internal class methods
  97. + (FIROptions *)defaultOptions {
  98. dispatch_once(&sDefaultOptionsOnceToken, ^{
  99. NSDictionary *defaultOptionsDictionary = [self defaultOptionsDictionary];
  100. if (defaultOptionsDictionary != nil) {
  101. sDefaultOptions =
  102. [[FIROptions alloc] initInternalWithOptionsDictionary:defaultOptionsDictionary];
  103. }
  104. });
  105. return sDefaultOptions;
  106. }
  107. #pragma mark - Private class methods
  108. + (NSDictionary *)defaultOptionsDictionary {
  109. dispatch_once(&sDefaultOptionsDictionaryOnceToken, ^{
  110. NSString *jsonFilePath = [FIROptions filePathWithName:kJsonFileName type:kJsonFileType];
  111. NSString *plistFilePath = [FIROptions filePathWithName:kServiceInfoFileName
  112. type:kServiceInfoFileType];
  113. if (jsonFilePath == nil && plistFilePath == nil) {
  114. return;
  115. }
  116. if (jsonFilePath != nil && plistFilePath != nil) {
  117. FIRLogError(kFIRLoggerCore, @"I-COR000015",
  118. @"Found both '%@.%@' and '%@.%@'."
  119. @"Ignoring the plist file and using the json file",
  120. kJsonFileName, kJsonFileType, kServiceInfoFileName, kServiceInfoFileType);
  121. }
  122. if (jsonFilePath != nil) {
  123. sDefaultOptionsDictionary = [self dictionaryFromJsonPath:jsonFilePath];
  124. } else {
  125. sDefaultOptionsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
  126. }
  127. if (sDefaultOptionsDictionary == nil) {
  128. FIRLogError(kFIRLoggerCore, @"I-COR000011",
  129. @"The configuration file is not a dictionary: "
  130. @"'%@'.",
  131. plistFilePath);
  132. }
  133. });
  134. return sDefaultOptionsDictionary;
  135. }
  136. /// Generate the options dictionary from a JSON file.
  137. + (NSDictionary *)dictionaryFromJsonPath:(NSString *)path {
  138. NSData *data = [NSData dataWithContentsOfFile:path];
  139. if (data == nil) {
  140. return nil;
  141. }
  142. NSError *error;
  143. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
  144. options:kNilOptions
  145. error:&error];
  146. if (error != nil) {
  147. FIRLogError(kFIRLoggerCore, @"I-COR000018", @"Decoding JSON: %@ at path: '%@.", error, path);
  148. return nil;
  149. }
  150. return dict;
  151. }
  152. // Returns the path of the file with a given file name and file type.
  153. + (NSString *)filePathWithName:(NSString *)fileName type:(NSString *)fileType {
  154. NSArray *bundles = [FIRBundleUtil relevantBundles];
  155. NSString *filePath = [FIRBundleUtil optionsDictionaryPathWithResourceName:fileName
  156. andFileType:fileType
  157. inBundles:bundles];
  158. return filePath;
  159. }
  160. + (void)resetDefaultOptions {
  161. sDefaultOptions = nil;
  162. sDefaultOptionsDictionary = nil;
  163. sDefaultOptionsOnceToken = 0;
  164. sDefaultOptionsDictionaryOnceToken = 0;
  165. }
  166. #pragma mark - Private instance methods
  167. - (instancetype)initInternalWithOptionsDictionary:(NSDictionary *)optionsDictionary {
  168. self = [super init];
  169. if (self) {
  170. _optionsDictionary = [optionsDictionary mutableCopy];
  171. _usingOptionsFromDefaultPlist = YES;
  172. if ([_optionsDictionary[kFIRJSONConfigVersion] isEqual:@"2"]) {
  173. _version = 2;
  174. } else {
  175. _version = 1;
  176. }
  177. }
  178. return self;
  179. }
  180. - (id)copyWithZone:(NSZone *)zone {
  181. FIROptions *newOptions = [(FIROptions *)[[self class] allocWithZone:zone]
  182. initInternalWithOptionsDictionary:self.optionsDictionary];
  183. if (newOptions) {
  184. newOptions.deepLinkURLScheme = self.deepLinkURLScheme;
  185. newOptions.appGroupID = self.appGroupID;
  186. newOptions.editingLocked = self.isEditingLocked;
  187. newOptions.usingOptionsFromDefaultPlist = self.usingOptionsFromDefaultPlist;
  188. }
  189. return newOptions;
  190. }
  191. #pragma mark - Public instance methods
  192. - (instancetype)init {
  193. // Unavailable.
  194. [self doesNotRecognizeSelector:_cmd];
  195. return nil;
  196. }
  197. - (instancetype)initWithContentsOfFile:(NSString *)path {
  198. self = [super init];
  199. if (self) {
  200. if (path == nil) {
  201. FIRLogError(kFIRLoggerCore, @"I-COR000013", @"The file path is nil.");
  202. return nil;
  203. }
  204. if ([path hasSuffix:@".json"]) {
  205. _optionsDictionary = [[FIROptions dictionaryFromJsonPath:path] mutableCopy];
  206. if (![_optionsDictionary[kFIRJSONConfigVersion] isEqual:@"2"]) {
  207. FIRLogError(kFIRLoggerCore, @"I-COR000016",
  208. @"Only version 2 is currently supported for JSON config files");
  209. return nil;
  210. }
  211. _version = 2;
  212. } else {
  213. _optionsDictionary = [[NSDictionary dictionaryWithContentsOfFile:path] mutableCopy];
  214. _version = 1;
  215. }
  216. if (_optionsDictionary == nil) {
  217. FIRLogError(kFIRLoggerCore, @"I-COR000014",
  218. @"The configuration file at %@ does not exist or "
  219. @"is not a well-formed plist file.",
  220. path);
  221. return nil;
  222. }
  223. // TODO: Do we want to validate the dictionary here? It says we do that already in
  224. // the public header.
  225. }
  226. return self;
  227. }
  228. - (instancetype)initWithGoogleAppID:(NSString *)googleAppID GCMSenderID:(NSString *)GCMSenderID {
  229. self = [super init];
  230. if (self) {
  231. NSMutableDictionary *mutableOptionsDict = [NSMutableDictionary dictionary];
  232. [mutableOptionsDict setValue:googleAppID forKey:kFIRGoogleAppID];
  233. [mutableOptionsDict setValue:GCMSenderID forKey:kFIRGCMSenderID];
  234. [mutableOptionsDict setValue:[[NSBundle mainBundle] bundleIdentifier] forKey:kFIRBundleID];
  235. self.optionsDictionary = mutableOptionsDict;
  236. _version = 1;
  237. }
  238. return self;
  239. }
  240. - (instancetype)initWithAppID:(NSString *)appID
  241. projectNumber:(NSString *)projectNumber
  242. projectID:(NSString *)projectID
  243. apiKey:(NSString *)apiKey {
  244. self = [super init];
  245. if (self) {
  246. NSMutableDictionary *mutableOptionsDict = [NSMutableDictionary dictionary];
  247. [mutableOptionsDict setValue:appID forKey:kFIRJSONConfigAppID];
  248. [mutableOptionsDict setValue:projectNumber forKey:kFIRJSONConfigProjectNumber];
  249. [mutableOptionsDict setValue:projectID forKey:kFIRJSONConfigProjectID];
  250. [mutableOptionsDict setValue:apiKey forKey:kFIRJSONConfigApiKey];
  251. [mutableOptionsDict setValue:@"2" forKey:kFIRJSONConfigVersion];
  252. [mutableOptionsDict setValue:[[NSBundle mainBundle] bundleIdentifier]
  253. forKey:kFIRJSONConfigBundleID];
  254. self.optionsDictionary = mutableOptionsDict;
  255. _version = 2;
  256. }
  257. return self;
  258. }
  259. - (NSString *)APIKey {
  260. NSString *key = _version == 2 ? kFIRJSONConfigApiKey : kFIRAPIKey;
  261. return self.optionsDictionary[key];
  262. }
  263. - (void)checkEditingLocked {
  264. if (self.isEditingLocked) {
  265. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  266. }
  267. }
  268. - (void)setAPIKey:(NSString *)APIKey {
  269. [self checkEditingLocked];
  270. NSString *key = _version == 2 ? kFIRJSONConfigApiKey : kFIRAPIKey;
  271. _optionsDictionary[key] = [APIKey copy];
  272. }
  273. - (NSString *)clientID {
  274. NSString *key = _version == 2 ? kFIRJSONConfigClientID : kFIRClientID;
  275. return self.optionsDictionary[key];
  276. }
  277. - (void)setClientID:(NSString *)clientID {
  278. [self checkEditingLocked];
  279. NSString *key = _version == 2 ? kFIRJSONConfigClientID : kFIRClientID;
  280. _optionsDictionary[key] = [clientID copy];
  281. }
  282. - (NSString *)trackingID {
  283. return self.optionsDictionary[kFIRTrackingID];
  284. }
  285. - (void)setTrackingID:(NSString *)trackingID {
  286. [self checkEditingLocked];
  287. _optionsDictionary[kFIRTrackingID] = [trackingID copy];
  288. }
  289. - (NSString *)projectNumber {
  290. NSString *key = _version == 2 ? kFIRJSONConfigProjectNumber : kFIRGCMSenderID;
  291. return self.optionsDictionary[key];
  292. }
  293. - (void)setProjectNumber:(NSString *)projectNumber {
  294. [self checkEditingLocked];
  295. NSString *key = _version == 2 ? kFIRJSONConfigProjectNumber : kFIRGCMSenderID;
  296. _optionsDictionary[key] = [projectNumber copy];
  297. }
  298. - (NSString *)GCMSenderID {
  299. return self.projectNumber;
  300. }
  301. - (void)setGCMSenderID:(NSString *)GCMSenderID {
  302. [self setProjectNumber:GCMSenderID];
  303. }
  304. - (NSString *)projectID {
  305. NSString *key = _version == 2 ? kFIRJSONConfigProjectID : kFIRProjectID;
  306. return self.optionsDictionary[key];
  307. }
  308. - (void)setProjectID:(NSString *)projectID {
  309. [self checkEditingLocked];
  310. NSString *key = _version == 2 ? kFIRJSONConfigProjectID : kFIRProjectID;
  311. _optionsDictionary[key] = [projectID copy];
  312. }
  313. - (NSString *)androidClientID {
  314. return self.optionsDictionary[kFIRAndroidClientID];
  315. }
  316. - (void)setAndroidClientID:(NSString *)androidClientID {
  317. [self checkEditingLocked];
  318. _optionsDictionary[kFIRAndroidClientID] = [androidClientID copy];
  319. }
  320. - (NSString *)googleAppID {
  321. NSString *key = _version == 2 ? kFIRJSONConfigAppID : kFIRGoogleAppID;
  322. return self.optionsDictionary[key];
  323. }
  324. - (void)setGoogleAppID:(NSString *)googleAppID {
  325. [self checkEditingLocked];
  326. NSString *key = _version == 2 ? kFIRJSONConfigAppID : kFIRGoogleAppID;
  327. _optionsDictionary[key] = [googleAppID copy];
  328. }
  329. - (NSString *)libraryVersionID {
  330. static dispatch_once_t onceToken;
  331. dispatch_once(&onceToken, ^{
  332. // The unit tests are set up to catch anything that does not properly convert.
  333. NSString *version = FIRFirebaseVersion();
  334. NSArray *components = [version componentsSeparatedByString:@"."];
  335. NSString *major = [NSString stringWithFormat:@"%02d", [[components objectAtIndex:0] intValue]];
  336. NSString *minor = [NSString stringWithFormat:@"%02d", [[components objectAtIndex:1] intValue]];
  337. NSString *patch = [NSString stringWithFormat:@"%02d", [[components objectAtIndex:2] intValue]];
  338. kFIRLibraryVersionID = [NSString stringWithFormat:@"%@%@%@000", major, minor, patch];
  339. });
  340. return kFIRLibraryVersionID;
  341. }
  342. - (void)setLibraryVersionID:(NSString *)libraryVersionID {
  343. _optionsDictionary[kFIRLibraryVersionID] = [libraryVersionID copy];
  344. }
  345. - (NSString *)databaseURL {
  346. NSString *key = _version == 2 ? kFIRJSONConfigRTDBURL : kFIRDatabaseURL;
  347. return self.optionsDictionary[key];
  348. }
  349. - (void)setDatabaseURL:(NSString *)databaseURL {
  350. [self checkEditingLocked];
  351. NSString *key = _version == 2 ? kFIRJSONConfigRTDBURL : kFIRDatabaseURL;
  352. _optionsDictionary[key] = [databaseURL copy];
  353. }
  354. - (NSString *)storageBucket {
  355. NSString *key = _version == 2 ? kFIRJSONConfigStorageBucket : kFIRStorageBucket;
  356. return self.optionsDictionary[key];
  357. }
  358. - (void)setStorageBucket:(NSString *)storageBucket {
  359. [self checkEditingLocked];
  360. NSString *key = _version == 2 ? kFIRJSONConfigStorageBucket : kFIRStorageBucket;
  361. _optionsDictionary[key] = [storageBucket copy];
  362. }
  363. - (void)setDeepLinkURLScheme:(NSString *)deepLinkURLScheme {
  364. [self checkEditingLocked];
  365. _deepLinkURLScheme = [deepLinkURLScheme copy];
  366. }
  367. - (NSString *)bundleID {
  368. NSString *key = _version == 2 ? kFIRJSONConfigBundleID : kFIRBundleID;
  369. return self.optionsDictionary[key];
  370. }
  371. - (void)setBundleID:(NSString *)bundleID {
  372. [self checkEditingLocked];
  373. NSString *key = _version == 2 ? kFIRJSONConfigBundleID : kFIRBundleID;
  374. _optionsDictionary[key] = [bundleID copy];
  375. }
  376. - (void)setAppGroupID:(NSString *)appGroupID {
  377. [self checkEditingLocked];
  378. _appGroupID = [appGroupID copy];
  379. }
  380. #pragma mark - Equality
  381. - (BOOL)isEqual:(id)object {
  382. if (!object || ![object isKindOfClass:[FIROptions class]]) {
  383. return NO;
  384. }
  385. return [self isEqualToOptions:(FIROptions *)object];
  386. }
  387. - (BOOL)isEqualToOptions:(FIROptions *)options {
  388. // Skip any non-FIROptions classes.
  389. if (![options isKindOfClass:[FIROptions class]]) {
  390. return NO;
  391. }
  392. // Check the internal dictionary and custom properties for differences.
  393. if (![options.optionsDictionary isEqualToDictionary:self.optionsDictionary]) {
  394. return NO;
  395. }
  396. // Validate extra properties not contained in the dictionary. Only validate it if one of the
  397. // objects has the property set.
  398. if ((options.deepLinkURLScheme != nil || self.deepLinkURLScheme != nil) &&
  399. ![options.deepLinkURLScheme isEqualToString:self.deepLinkURLScheme]) {
  400. return NO;
  401. }
  402. if ((options.appGroupID != nil || self.appGroupID != nil) &&
  403. ![options.appGroupID isEqualToString:self.appGroupID]) {
  404. return NO;
  405. }
  406. // Validate the Analytics options haven't changed with the Info.plist.
  407. if (![options.analyticsOptionsDictionary isEqualToDictionary:self.analyticsOptionsDictionary]) {
  408. return NO;
  409. }
  410. // We don't care about the `editingLocked` or `usingOptionsFromDefaultPlist` properties since
  411. // those relate to lifecycle and construction, we only care if the contents of the options
  412. // themselves are equal.
  413. return YES;
  414. }
  415. - (NSUInteger)hash {
  416. // This is strongly recommended for any object that implements a custom `isEqual:` method to
  417. // ensure that dictionary and set behavior matches other `isEqual:` checks.
  418. // Note: `self.analyticsOptionsDictionary` was left out here since it solely relies on the
  419. // contents of the main bundle's `Info.plist`. We should avoid reading that file and the contents
  420. // should be identical.
  421. return self.optionsDictionary.hash ^ self.deepLinkURLScheme.hash ^ self.appGroupID.hash;
  422. }
  423. #pragma mark - Internal instance methods
  424. - (NSDictionary *)analyticsOptionsDictionaryWithInfoDictionary:(NSDictionary *)infoDictionary {
  425. if (_analyticsOptionsDictionary == nil) {
  426. NSMutableDictionary *tempAnalyticsOptions = [[NSMutableDictionary alloc] init];
  427. NSArray *measurementKeys = @[
  428. kFIRIsMeasurementEnabled, kFIRIsAnalyticsCollectionEnabled,
  429. kFIRIsAnalyticsCollectionDeactivated
  430. ];
  431. for (NSString *key in measurementKeys) {
  432. id value = infoDictionary[key] ?: self.optionsDictionary[key] ?: nil;
  433. if (!value) {
  434. continue;
  435. }
  436. tempAnalyticsOptions[key] = value;
  437. }
  438. _analyticsOptionsDictionary = tempAnalyticsOptions;
  439. }
  440. return _analyticsOptionsDictionary;
  441. }
  442. - (NSDictionary *)analyticsOptionsDictionary {
  443. return [self analyticsOptionsDictionaryWithInfoDictionary:[NSBundle mainBundle].infoDictionary];
  444. }
  445. /**
  446. * Whether or not Measurement was enabled. Measurement is enabled unless explicitly disabled in
  447. * GoogleService-Info.plist. This uses the old plist flag IS_MEASUREMENT_ENABLED, which should still
  448. * be supported.
  449. */
  450. - (BOOL)isMeasurementEnabled {
  451. if (self.isAnalyticsCollectionDeactivated) {
  452. return NO;
  453. }
  454. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled];
  455. if (value == nil) {
  456. // TODO: This could probably be cleaned up since FIROptions shouldn't know about FIRApp or have
  457. // to check if it's the default app. The FIROptions instance can't be modified after
  458. // `+configure` is called, so it's not a good place to copy it either in case the flag is
  459. // changed at runtime.
  460. // If no values are set for Analytics, fall back to the global collection switch in FIRApp.
  461. // Analytics only supports the default FIRApp, so check that first.
  462. if (![FIRApp isDefaultAppConfigured]) {
  463. return NO;
  464. }
  465. // Fall back to the default app's collection switch when the key is not in the dictionary.
  466. return [FIRApp defaultApp].isDataCollectionDefaultEnabled;
  467. }
  468. return [value boolValue];
  469. }
  470. - (BOOL)isAnalyticsCollectionExplicitlySet {
  471. // If it's de-activated, it classifies as explicitly set. If not, it's not a good enough
  472. // indication that the developer wants FirebaseAnalytics enabled so continue checking.
  473. if (self.isAnalyticsCollectionDeactivated) {
  474. return YES;
  475. }
  476. // Check if the current Analytics flag is set.
  477. id collectionEnabledObject = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled];
  478. if (collectionEnabledObject && [collectionEnabledObject isKindOfClass:[NSNumber class]]) {
  479. // It doesn't matter what the value is, it's explicitly set.
  480. return YES;
  481. }
  482. // Check if the old measurement flag is set.
  483. id measurementEnabledObject = self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled];
  484. if (measurementEnabledObject && [measurementEnabledObject isKindOfClass:[NSNumber class]]) {
  485. // It doesn't matter what the value is, it's explicitly set.
  486. return YES;
  487. }
  488. // No flags are set to explicitly enable or disable FirebaseAnalytics.
  489. return NO;
  490. }
  491. - (BOOL)isAnalyticsCollectionEnabled {
  492. if (self.isAnalyticsCollectionDeactivated) {
  493. return NO;
  494. }
  495. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled];
  496. if (value == nil) {
  497. return self.isMeasurementEnabled; // Fall back to older plist flag.
  498. }
  499. return [value boolValue];
  500. }
  501. - (BOOL)isAnalyticsCollectionDeactivated {
  502. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionDeactivated];
  503. if (value == nil) {
  504. return NO; // Analytics Collection is not deactivated when the key is not in the dictionary.
  505. }
  506. return [value boolValue];
  507. }
  508. @end