Instantly share code, notes, and snippets.

Embed
What would you like to do?
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