Skip to content

Instantly share code, notes, and snippets.

@fernandomatal
Created February 6, 2018 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandomatal/2bca9cfd0d45e3770daef06484f643b0 to your computer and use it in GitHub Desktop.
Save fernandomatal/2bca9cfd0d45e3770daef06484f643b0 to your computer and use it in GitHub Desktop.
Override values from Info.plist while building the project
#!/usr/bin/env xcrun --sdk macosx swift
// Use this gist in order to write values in your Info.plist as a run phase
// Remember to add a custom Build Phase in your targets running this file as a first step
import Foundation
let relativePlistFilesFolder = "/YOUR_INFO_LIST_FOLDER/"
let currentDirectoryPath = FileManager.default.currentDirectoryPath
func readPlistFile(atPath path: String) -> [String : Any] {
var plistData: [String : Any] = [:]
guard let plistFileData = FileManager.default.contents(atPath: path) else {
print("File doesn't exist")
exit(1)
}
do {
plistData = try PropertyListSerialization.propertyList(from: plistFileData,
options: [],
format: nil
) as! [String : Any]
}
catch {
print("Error fetching list file")
}
return plistData
}
func writePlistFile(withData data: NSDictionary, atPath path: String) -> Bool {
guard FileManager.default.fileExists(atPath: path) else {
return false
}
return data.write(toFile: path, atomically: false)
}
// Create plist path and read data
let infoListPath = currentDirectoryPath + relativePlistFilesFolder + "Info.plist"
var plistData: [String : Any] = readPlistFile(atPath: infoListPath)
// Replace the values you want
// Remember that the dictionary contains the raw key
plistData["CFBundleVersion"] = 1 // Update existing values
plistData["YOUR_CUSTOM_KEY"] = "Value" // Add custom values
// Write data
if !writePlistFile(withData: plistData as NSDictionary, atPath: infoListPath) {
print("Error writing list \(infoListPath)")
exit(1)
}
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment