Skip to content

Instantly share code, notes, and snippets.

@TheWorstProgrammerEver
Created March 5, 2022 08:12
Show Gist options
  • Save TheWorstProgrammerEver/4876d3729188ceb29d74c9c130b8c580 to your computer and use it in GitHub Desktop.
Save TheWorstProgrammerEver/4876d3729188ceb29d74c9c130b8c580 to your computer and use it in GitHub Desktop.
Swift Info.plist as JSON, injectable via Environment for SwiftUI
struct InfoPListEnvironmentKey : EnvironmentKey {
private static var value: InfoPList = .load()
static var defaultValue: InfoPList { value }
}
extension EnvironmentValues {
var infoPList: InfoPList {
get {
self[InfoPListEnvironmentKey.self]
}
set {
fatalError("Don't override this.")
}
}
}
import Foundation
struct InfoPList : Codable {
var CFBundleIdentifier: String
var CFBundleShortVersionString: String
var CFBundleVersion: String
// etc - match on names.
// ⚠️ Beware - if the names change between versions of the SDK, obviously this will break.
var FormattedVersion: String {
"\(CFBundleShortVersionString).\(CFBundleVersion)"
}
}
extension InfoPList {
static func load() -> Self {
let rawData = try! JSONSerialization.data(withJSONObject: Bundle.main.infoDictionary!)
return try! JSONDecoder().decode(InfoPList.self, from: rawData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment