[ iOS / Swift ] 如何從 Info.plist 裡取得 App 專案的名稱與版本號
實作:
2. 由於回傳的是字典 [String : Any]?,需要填寫 Key 值,才有辦法取得對應的資訊,然後再透過 as? 將資訊轉型成字串。
override func viewDidLoad() {
super.viewDidLoad()
if let infoDictionary = Bundle.main.infoDictionary {
let bundleName = infoDictionary["CFBundleName"] as? String
let bundleVersionString = infoDictionary["CFBundleShortVersionString"] as? String
let bundleVersion = infoDictionary["CFBundleVersion"] as? String
print(bundleName ?? "") // Test 測試專用
print(bundleVersionString ?? "") // 2.0.1
print(bundleVersion ?? "") // 1
}
}
override func viewDidLoad() {
super.viewDidLoad()
if let infoDictionary = Bundle.main.infoDictionary {
let bundleName = infoDictionary[kCFBundleNameKey as String] as? String
let bundleVersionString = infoDictionary["CFBundleShortVersionString"] as? String
let bundleVersion = infoDictionary[kCFBundleVersionKey as String] as? String
print(bundleName ?? "") // Test 測試專用
print(bundleVersionString ?? "") // 2.0.1
print(bundleVersion ?? "") // 1
}
}
留言
張貼留言