0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Swift5でバージョンとビルド情報を取得する

Posted at

アプリ関連の文字列を1か所にまとめたSwift5用のAppInfoという構造体(struct)を用意する。

AppInfoの構造体(struct)

AppInfo.swift
struct AppInfo {
    var appName : String {
        return readFromInfoPlist(withKey: "CFBundleName") ?? "(unknown app name)"
    }

    var appDisplayName : String {
        return readFromInfoPlist(withKey: "CFBundleDisplayName") ?? "(unknown app displayname)"
    }

    var version : String {
        return readFromInfoPlist(withKey: "CFBundleShortVersionString") ?? "(unknown app version)"
    }

    var build : String {
        return readFromInfoPlist(withKey: "CFBundleVersion") ?? "(unknown build number)"
    }

    var minimumOSVersion : String {
        return readFromInfoPlist(withKey: "MinimumOSVersion") ?? "(unknown minimum OSVersion)"
    }

    var copyrightNotice : String {
        return readFromInfoPlist(withKey: "NSHumanReadableCopyright") ?? "(unknown copyright notice)"
    }

    var bundleIdentifier : String {
        return readFromInfoPlist(withKey: "CFBundleIdentifier") ?? "(unknown bundle identifier)"
    }

    // Info.plistへの参照をDictionaryとして保持
    private let infoPlistDictionary = Bundle.main.infoDictionary

    /// info.Plistから関連する値 (文字列型)を取得
    private func readFromInfoPlist(withKey key: String) -> String? {
        return infoPlistDictionary?[key] as? String
    }
}


こんな感じで使えます

        let appInfo = AppInfo()
        print(appInfo.appName)
        print(appInfo.appDisplayName)
        print(appInfo.version)

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?