ApplicationInfo.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Copyright 2022 Google LLC
  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. import Foundation
  16. @_implementationOnly import FirebaseCore
  17. @_implementationOnly import GoogleUtilities
  18. /// Development environment for the application.
  19. enum DevEnvironment: String {
  20. case prod // Prod environment
  21. case staging // Staging environment
  22. case autopush // Autopush environment
  23. }
  24. protocol ApplicationInfoProtocol {
  25. /// Google App ID / GMP App ID
  26. var appID: String { get }
  27. /// App's bundle ID / bundle short version
  28. var bundleID: String { get }
  29. /// Version of the Firebase SDK
  30. var sdkVersion: String { get }
  31. /// Crashlytics-specific device / OS filter values.
  32. var osName: String { get }
  33. /// Model of the device
  34. var deviceModel: String { get }
  35. /// Validated Mobile Country Code and Mobile Network Code
  36. var mccMNC: String { get }
  37. /// Development environment on which the application is running.
  38. var environment: DevEnvironment { get }
  39. var appBuildVersion: String { get }
  40. var appDisplayVersion: String { get }
  41. }
  42. class ApplicationInfo: ApplicationInfoProtocol {
  43. let appID: String
  44. private let networkInfo: NetworkInfoProtocol
  45. private let envParams: [String: String]
  46. init(appID: String, networkInfo: NetworkInfoProtocol = NetworkInfo(),
  47. envParams: [String: String] = ProcessInfo.processInfo.environment) {
  48. self.appID = appID
  49. self.networkInfo = networkInfo
  50. self.envParams = envParams
  51. }
  52. var bundleID: String {
  53. return Bundle.main.bundleIdentifier ?? ""
  54. }
  55. var sdkVersion: String {
  56. return FirebaseVersion()
  57. }
  58. var osName: String {
  59. return GULAppEnvironmentUtil.appleDevicePlatform()
  60. }
  61. var deviceModel: String {
  62. #if targetEnvironment(simulator)
  63. return GULAppEnvironmentUtil.deviceSimulatorModel() ?? ""
  64. #else
  65. return GULAppEnvironmentUtil.deviceModel() ?? ""
  66. #endif // targetEnvironment(simulator)
  67. }
  68. var mccMNC: String {
  69. return FIRSESValidateMccMnc(networkInfo.mobileCountryCode, networkInfo.mobileNetworkCode) ?? ""
  70. }
  71. var environment: DevEnvironment {
  72. if let environment = envParams["FirebaseSessionsRunEnvironment"] {
  73. return DevEnvironment(rawValue: environment.trimmingCharacters(in: .whitespaces).lowercased())
  74. ?? DevEnvironment.prod
  75. }
  76. return DevEnvironment.prod
  77. }
  78. var appBuildVersion: String {
  79. return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  80. }
  81. var appDisplayVersion: String {
  82. return Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? ""
  83. }
  84. }