はじめに
アプリ内でこのような表示を見たことはありませんか?
これはXcodeで設定できるIdentityのVersionとBuildで構成されているアプリのバージョン情報です。
バージョンの設定をどのように取得するのでしょうか
今回はやり方を紹介します
方法
Version
var version: String {
guard let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String else { return "" }
return version
}
Build
var build: String {
guard let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String else { return "" }
return build
}
使い方
import SwiftUI
struct ContentView: View {
var version: String {
guard let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String else { return "" }
return version
}
var build: String {
guard let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String else { return "" }
return build
}
var body: some View {
List {
HStack {
Text("バージョン")
.fontWeight(.bold)
Spacer()
Text("\(version)(\(build))")
.foregroundColor(.gray)
.fontWeight(.regular)
}
}
}
}