Skip to content

Instantly share code, notes, and snippets.

@jagreenwood
Last active February 26, 2018 16:17
Show Gist options
  • Save jagreenwood/ddc2e8acf9fed3dfbfd0e453152e5f8e to your computer and use it in GitHub Desktop.
Save jagreenwood/ddc2e8acf9fed3dfbfd0e453152e5f8e to your computer and use it in GitHub Desktop.
Version Settings Bundle

This Gist explains how to set up an Xcode proj to show the app version and the last six characters of the lastest git commit message.

  1. Add contents of SettingsAccess.swift to the Xcode project.
  2. Add a Settings bundle to the app, then configure Root.plist like displayed here.
  3. Add a Run Script build phase to the Xcode project. Copy the contents of CommitHash.sh to phase.
#!/bin/bash
set -o errexit
set -o nounset
INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Info"
COMMIT_HASH=$(git rev-parse HEAD)
defaults write "$INFO_PLIST" CommitHash $COMMIT_HASH
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSTitleValueSpecifier</string>
<key>Key</key>
<string>app_version</string>
<key>DefaultValue</key>
<string></string>
</dict>
</array>
</dict>
</plist>
import Foundation
struct SettingsAccess {
fileprivate static let appVersionKey = "app_version"
static func initialize() {
if let shortVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String, let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String, let commit = Bundle.main.object(forInfoDictionaryKey: "CommitHash") as? String {
let trimmedCommit = commit[commit.index(commit.endIndex, offsetBy: -6)...]
UserDefaults.standard.set("\(shortVersion)(\(version)) \(trimmedCommit)", forKey: appVersionKey)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment