Created
July 9, 2023 13:02
-
-
Save thomsmed/f95e198785c00fdc0e57ca2b22999e95 to your computer and use it in GitHub Desktop.
AppInfoProvider.swift - A basic wrapper around your app's Info.plist and more
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
// | |
// AppInfoProvider.swift | |
// | |
import Foundation | |
protocol AppInfoProvider: AnyObject { | |
var bundleDisplayName: String { get } // Your app's name. | |
var bundleVersion: String { get } // The current build number, e.g 137 | |
var bundleShortVersionString: String { get } // The current app version, e.g 1.2.0. | |
var primaryAppIconName: String { get } // Name of the asset representing the primary app icon. | |
var alternateAppIconNames: [String] { get } // List of names of assets representing the alternate app icons. | |
} | |
final class DefaultAppInfoProvider: AppInfoProvider { | |
private func getValue<Value>(for key: String) -> Value { | |
guard | |
let value = Bundle.main.infoDictionary?[key] as? Value | |
else { | |
fatalError("Missing value for \(key) in Info.plist") | |
} | |
return value | |
} | |
private func getPrimaryAppIconName() -> String { | |
let appIconsDict: [String: [String: Any]] = getValue(for: "CFBundleIcons") | |
let primaryIconDict = appIconsDict["CFBundlePrimaryIcon"] | |
guard let primaryIconName = primaryIconDict?["CFBundleIconName"] as? String else { | |
fatalError("Missing primary icon name") | |
} | |
return primaryIconName | |
} | |
private func getAlternateAppIconNames() -> [String] { | |
let appIconsDict: [String: [String: Any]] = getValue(for: "CFBundleIcons") | |
let alternateIconsDict = appIconsDict["CFBundleAlternateIcons"] as? [String: [String: String]] | |
var alternateAppIconNames = [String]() | |
alternateIconsDict?.forEach { _, value in | |
if let alternateIconName = value["CFBundleIconName"] { | |
alternateAppIconNames.append(alternateIconName) | |
} | |
} | |
return alternateAppIconNames | |
} | |
lazy var bundleDisplayName: String = getValue(for: "CFBundleDisplayName") | |
lazy var bundleVersion: String = getValue(for: "CFBundleVersion") | |
lazy var bundleShortVersionString: String = getValue(for: "CFBundleShortVersionString") | |
lazy var primaryAppIconName: String = getPrimaryAppIconName() | |
lazy var alternateAppIconNames: [String] = getAlternateAppIconNames() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment