7
2

More than 3 years have passed since last update.

iOSでgitのcommit hashを取得し表示する

Last updated at Posted at 2020-04-15

iOSアプリを開発していて、アプリの画面内にバージョン情報を表記することがあると思います。
この際、gitのcommit hashを表示したかったので、方法をまとめました。
こんな感じでバージョン情報を表記できます。
1.0.0_d6d4de6

Xcodeのバージョンは Version 11.4 (11E146) です。

Info.plistの編集

Info.plistCommitHash というkeyを追加します。
valueには undefine というStringを入れておきます。
Info.plist

Build Settingsの編集

Build Settings の Preprocess Info.plist FileYES に変更します。
BuildSettings

Build Phasesの編集

Build Phases の Compile Sources の前に Run Script Phase を追加します。
今回は Get Commit Hash という名前で作成しています。

コマンドは下記を追加します。

plistBuddy="/usr/libexec/PlistBuddy"
infoPlistFile="${TEMP_DIR}/Preprocessed-Info.plist"

commitHash=$(git rev-parse --short HEAD)
$plistBuddy -c "Set :CommitHash $commitHash" $infoPlistFile

Input Filesの項目に
${TEMP_DIR}/Preprocessed-Info.plist
を設定します。

下記画像のような設定になっていればOKです!
BuildPhases

コードからバージョン情報とcommit hashを呼び出す

swiftのコードからバージョン情報とcommit hashを呼び出すには下記のようにします。

let info = Bundle.main.infoDictionary!
let versionName = "\(info["CFBundleShortVersionString"]!)_\(info["CommitHash"]!)"

以上です!

7
2
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
7
2