0
1

More than 1 year has passed since last update.

【Swift】アプリのバージョン情報を表示

Posted at

はじめに

アプリ内でこのような表示を見たことはありませんか?
IMG_0579.jpg
これはXcodeで設定できるIdentityのVersionとBuildで構成されているアプリのバージョン情報です。
スクリーンショット 2022-07-01 15.16.30.png

バージョンの設定をどのように取得するのでしょうか

今回はやり方を紹介します

方法

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
}

使い方

Simulator Screen Shot - iPhone 12 - 2022-07-01 at 15.25.18.png

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)
            }
        }
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1