FIRIAMClientInfoFetcher.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <FirebaseCore/FIRAppInternal.h>
  17. #import <FirebaseCore/FIRLogger.h>
  18. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  19. #import "FIRCore+InAppMessaging.h"
  20. #import "FIRIAMClientInfoFetcher.h"
  21. // declaratons for FIRInstanceID SDK
  22. @implementation FIRIAMClientInfoFetcher
  23. - (void)fetchFirebaseIIDDataWithProjectNumber:(NSString *)projectNumber
  24. withCompletion:(void (^)(NSString *_Nullable iid,
  25. NSString *_Nullable token,
  26. NSError *_Nullable error))completion {
  27. FIRInstanceID *iid = [FIRInstanceID instanceID];
  28. // tokenWithAuthorizedEntity would only communicate with server on periodical cycles.
  29. // For other times, it's going to fetch from local cache, so it's not causing any performance
  30. // concern in the fetch flow.
  31. [iid tokenWithAuthorizedEntity:projectNumber
  32. scope:@"fiam"
  33. options:nil
  34. handler:^(NSString *_Nullable token, NSError *_Nullable error) {
  35. if (error) {
  36. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM190001",
  37. @"Error in fetching iid token: %@",
  38. error.localizedDescription);
  39. completion(nil, nil, error);
  40. } else {
  41. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM190002",
  42. @"Successfully generated iid token");
  43. // now we can go ahead to fetch the id
  44. [iid getIDWithHandler:^(NSString *_Nullable identity,
  45. NSError *_Nullable error) {
  46. if (error) {
  47. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM190004",
  48. @"Error in fetching iid value: %@",
  49. error.localizedDescription);
  50. } else {
  51. FIRLogDebug(
  52. kFIRLoggerInAppMessaging, @"I-IAM190005",
  53. @"Successfully in fetching both iid value as %@ and iid token"
  54. " as %@",
  55. identity, token);
  56. completion(identity, token, nil);
  57. }
  58. }];
  59. }
  60. }];
  61. }
  62. - (nullable NSString *)getDeviceLanguageCode {
  63. // No caching since it's requested at pretty low frequency and we get the benefit of seeing
  64. // updated info the setting has changed
  65. NSArray<NSString *> *preferredLanguages = [NSLocale preferredLanguages];
  66. return preferredLanguages.firstObject;
  67. }
  68. - (nullable NSString *)getAppVersion {
  69. // Since this won't change, read it once in the whole life-cycle of the app and cache its value
  70. static NSString *appVersion = nil;
  71. static dispatch_once_t onceToken;
  72. dispatch_once(&onceToken, ^{
  73. appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
  74. });
  75. return appVersion;
  76. }
  77. - (nullable NSString *)getOSVersion {
  78. // Since this won't change, read it once in the whole life-cycle of the app and cache its value
  79. static NSString *OSVersion = nil;
  80. static dispatch_once_t onceToken;
  81. dispatch_once(&onceToken, ^{
  82. NSOperatingSystemVersion systemVersion = [NSProcessInfo processInfo].operatingSystemVersion;
  83. OSVersion = [NSString stringWithFormat:@"%ld.%ld.%ld", (long)systemVersion.majorVersion,
  84. (long)systemVersion.minorVersion,
  85. (long)systemVersion.patchVersion];
  86. });
  87. return OSVersion;
  88. }
  89. - (nullable NSString *)getOSMajorVersion {
  90. NSArray *versionItems = [[self getOSVersion] componentsSeparatedByString:@"."];
  91. if (versionItems.count > 0) {
  92. return (NSString *)versionItems[0];
  93. } else {
  94. return nil;
  95. }
  96. }
  97. - (nullable NSString *)getTimezone {
  98. // No caching to deal with potential changes.
  99. return [NSTimeZone localTimeZone].name;
  100. }
  101. // extract macro value into a C string
  102. #define STR_FROM_MACRO(x) #x
  103. #define STR(x) STR_FROM_MACRO(x)
  104. - (NSString *)getIAMSDKVersion {
  105. // FIRInAppMessaging_LIB_VERSION macro comes from pod definition
  106. return [NSString stringWithUTF8String:STR(FIRInAppMessaging_LIB_VERSION)];
  107. }
  108. @end