FIRIAMClientInfoFetcher.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS || TARGET_OS_TV
  18. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  19. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  20. #import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
  21. #import "FirebaseInAppMessaging/Sources/FIRInAppMessagingPrivate.h"
  22. #import "FirebaseInAppMessaging/Sources/Private/Analytics/FIRIAMClientInfoFetcher.h"
  23. #import "FirebaseInAppMessaging/Sources/Runtime/FIRIAMSDKRuntimeErrorCodes.h"
  24. @interface FIRIAMClientInfoFetcher ()
  25. @property(nonatomic, strong, nullable, readonly) FIRInstallations *installations;
  26. @end
  27. @implementation FIRIAMClientInfoFetcher
  28. - (instancetype)initWithFirebaseInstallations:(FIRInstallations *)installations {
  29. if (self = [super init]) {
  30. _installations = installations;
  31. }
  32. return self;
  33. }
  34. - (void)fetchFirebaseInstallationDataWithProjectNumber:(NSString *)projectNumber
  35. withCompletion:
  36. (void (^)(NSString *_Nullable FID,
  37. NSString *_Nullable FISToken,
  38. NSError *_Nullable error))completion {
  39. if (!self.installations) {
  40. NSString *errorDesc = @"Couldn't generate Firebase Installation info";
  41. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM190010", @"%@", errorDesc);
  42. NSError *error = [NSError errorWithDomain:kFirebaseInAppMessagingErrorDomain
  43. code:FIRIAMSDKRuntimeErrorNoFirebaseInstallationsObject
  44. userInfo:@{NSLocalizedDescriptionKey : errorDesc}];
  45. completion(nil, nil, error);
  46. return;
  47. }
  48. [self.installations authTokenWithCompletion:^(
  49. FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  50. NSError *_Nullable error) {
  51. if (error) {
  52. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM190006", @"Error in fetching FIS token: %@",
  53. error.localizedDescription);
  54. completion(nil, nil, error);
  55. } else {
  56. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM190007", @"Successfully generated FIS token");
  57. [self.installations
  58. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  59. if (error) {
  60. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM190008", @"Error in fetching FID: %@",
  61. error.localizedDescription);
  62. completion(nil, tokenResult.authToken, error);
  63. } else {
  64. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM190009",
  65. @"Successfully in fetching both FID as %@ and FIS token as %@",
  66. identifier, tokenResult.authToken);
  67. completion(identifier, tokenResult.authToken, nil);
  68. }
  69. }];
  70. }
  71. }];
  72. }
  73. - (nullable NSString *)getDeviceLanguageCode {
  74. // No caching since it's requested at pretty low frequency and we get the benefit of seeing
  75. // updated info the setting has changed
  76. NSArray<NSString *> *preferredLanguages = [NSLocale preferredLanguages];
  77. return preferredLanguages.firstObject;
  78. }
  79. - (nullable NSString *)getAppVersion {
  80. // Since this won't change, read it once in the whole life-cycle of the app and cache its value
  81. static NSString *appVersion = nil;
  82. static dispatch_once_t onceToken;
  83. dispatch_once(&onceToken, ^{
  84. appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
  85. });
  86. return appVersion;
  87. }
  88. - (nullable NSString *)getOSVersion {
  89. // Since this won't change, read it once in the whole life-cycle of the app and cache its value
  90. static NSString *OSVersion = nil;
  91. static dispatch_once_t onceToken;
  92. dispatch_once(&onceToken, ^{
  93. NSOperatingSystemVersion systemVersion = [NSProcessInfo processInfo].operatingSystemVersion;
  94. OSVersion = [NSString stringWithFormat:@"%ld.%ld.%ld", (long)systemVersion.majorVersion,
  95. (long)systemVersion.minorVersion,
  96. (long)systemVersion.patchVersion];
  97. });
  98. return OSVersion;
  99. }
  100. - (nullable NSString *)getOSMajorVersion {
  101. NSArray *versionItems = [[self getOSVersion] componentsSeparatedByString:@"."];
  102. if (versionItems.count > 0) {
  103. return (NSString *)versionItems[0];
  104. } else {
  105. return nil;
  106. }
  107. }
  108. - (nullable NSString *)getTimezone {
  109. // No caching to deal with potential changes.
  110. return [NSTimeZone localTimeZone].name;
  111. }
  112. - (NSString *)getIAMSDKVersion {
  113. // FIRInAppMessaging_LIB_VERSION macro comes from pod definition
  114. return FIRFirebaseVersion();
  115. }
  116. @end
  117. #endif // TARGET_OS_IOS || TARGET_OS_TV