アプリ関連の文字列を1か所にまとめたSwift5用のAppInfo
という構造体(struct)を用意する。
AppInfoの構造体(struct)
AppInfo.swift
struct AppInfo {
var appName : String {
return readFromInfoPlist(withKey: "CFBundleName") ?? "(unknown app name)"
}
var appDisplayName : String {
return readFromInfoPlist(withKey: "CFBundleDisplayName") ?? "(unknown app displayname)"
}
var version : String {
return readFromInfoPlist(withKey: "CFBundleShortVersionString") ?? "(unknown app version)"
}
var build : String {
return readFromInfoPlist(withKey: "CFBundleVersion") ?? "(unknown build number)"
}
var minimumOSVersion : String {
return readFromInfoPlist(withKey: "MinimumOSVersion") ?? "(unknown minimum OSVersion)"
}
var copyrightNotice : String {
return readFromInfoPlist(withKey: "NSHumanReadableCopyright") ?? "(unknown copyright notice)"
}
var bundleIdentifier : String {
return readFromInfoPlist(withKey: "CFBundleIdentifier") ?? "(unknown bundle identifier)"
}
// Info.plistへの参照をDictionaryとして保持
private let infoPlistDictionary = Bundle.main.infoDictionary
/// info.Plistから関連する値 (文字列型)を取得
private func readFromInfoPlist(withKey key: String) -> String? {
return infoPlistDictionary?[key] as? String
}
}
こんな感じで使えます
let appInfo = AppInfo()
print(appInfo.appName)
print(appInfo.appDisplayName)
print(appInfo.version)
参考