Created
March 5, 2022 08:12
-
-
Save TheWorstProgrammerEver/4876d3729188ceb29d74c9c130b8c580 to your computer and use it in GitHub Desktop.
Swift Info.plist as JSON, injectable via Environment for SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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