Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. import Foundation
  2.  
  3. public enum Environment: String {
  4.  
  5. // A plist should exist for each of the environments
  6. // below. For example `case production = "Prod"` will
  7. // have a corresponding `Prod.plist`.
  8.  
  9. case production
  10. case staging
  11. case development
  12.  
  13. // Below represents a key found in each/every plist
  14. // file mentioned above
  15.  
  16. public enum Key: String {
  17.  
  18. case baseUrl = "BASE_URL"
  19. case socialToken = "SOCIAL_TOKEN"
  20. }
  21.  
  22. fileprivate class EnvBundleClass { }
  23.  
  24. private static var bundle: Bundle? = {
  25.  
  26. return Bundle(for: EnvBundleClass.self)
  27. }()
  28.  
  29. public static var versionNumber: String? {
  30.  
  31. guard let version = bundle?.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String else {
  32.  
  33. return nil
  34. }
  35.  
  36. return version
  37. }
  38.  
  39. public static var buildNumber: String? {
  40.  
  41. guard let build = bundle?.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String else {
  42.  
  43. return nil
  44. }
  45.  
  46. return build
  47. }
  48.  
  49. public static var current: Environment = {
  50.  
  51. var raw: String?
  52.  
  53. if let currentConfig = bundle?.object(forInfoDictionaryKey: "Config") as? String {
  54.  
  55. raw = currentConfig
  56. .replacingOccurrences(of: "Debug", with: "")
  57. .replacingOccurrences(of: "Release", with: "")
  58. .replacingOccurrences(of: "-", with: "")
  59. }
  60.  
  61. if let raw = raw, let environment = Environment(rawValue: raw) {
  62.  
  63. return environment
  64. }
  65.  
  66. return .production
  67. }()
  68.  
  69. public static var plistPath: String? {
  70.  
  71. return Environment.bundle?.path(forResource: current.rawValue, ofType: "plist")
  72. }
  73.  
  74. public static var plist: Dictionary<String, Any>? = {
  75.  
  76. guard let plistPath = plistPath else {
  77.  
  78. return nil
  79. }
  80.  
  81. return NSDictionary(contentsOfFile: plistPath) as? Dictionary<String, Any>
  82. }()
  83.  
  84. // MARK: - Convenience
  85.  
  86. public static func value<T>(for key: Environment.Key) -> T? {
  87.  
  88. return plist?[key.rawValue] as? T
  89. }
  90.  
  91. public static func int(for key: Environment.Key) -> Int? {
  92.  
  93. return plist?[key.rawValue] as? Int
  94. }
  95.  
  96. public static func string(for key: Environment.Key) -> String? {
  97.  
  98. return plist?[key.rawValue] as? String
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement