LoginSignup
38
30

More than 3 years have passed since last update.

[Swift5] iOSアプリのバージョンとビルドを取得し、ラベルに表示する

Last updated at Posted at 2017-05-29

概要

SwiftでiOSアプリのバージョンとビルド情報を取得する方法の備忘録。
VersionとBuildとは、TARGETS -> General -> Identity の下2行のこと。

バージョンとビルド情報を取得

ViewController.swift
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String

let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String

ラベルに表示する場合

ViewController.swift
// Storyboard上のLabel等にOutletを繋ぐ
@IBOutlet weak var versionLabel: UILabel!

override func viewDidLoad() {

    versionLabel.text = "\(version) (\(build))"
    // こんな風に表示されます -> 1.0.0 (1)
}

僕はTableViewCellのラベルで表示するのに使いました。

適宜、心配な方はオプショナルにしてデフォルトを設定するなどするとより安全かもしれません。

(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String) ?? "Unknown"

Androidでは?

FlutterでAndroidも調べたところ、以下のような対比になっているようですね。

versionName = CFBundleShortVersionString
versionCode = CFBundleVersion

参考元

iOSアプリのバージョンを取得する
AndroidとiOSのアプリバージョン周りについてまとめてみた

38
30
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
38
30