Created
February 6, 2018 10:37
-
-
Save fernandomatal/2bca9cfd0d45e3770daef06484f643b0 to your computer and use it in GitHub Desktop.
Override values from Info.plist while building the project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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