From 4aa9921822317f1b16ad4de5574d5753b75cb24f Mon Sep 17 00:00:00 2001 From: ned Date: Tue, 2 Jul 2019 16:08:25 +0200 Subject: [PATCH] replace bundle id in all Info.plist files found --- .../Downloads/Library/IPAFileManager.swift | 28 ++++++++++++------- .../Downloads/Library/Library+Extension.swift | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/appdb/Tabs/Downloads/Library/IPAFileManager.swift b/appdb/Tabs/Downloads/Library/IPAFileManager.swift index 309d46ec..d1e700b1 100644 --- a/appdb/Tabs/Downloads/Library/IPAFileManager.swift +++ b/appdb/Tabs/Downloads/Library/IPAFileManager.swift @@ -213,7 +213,7 @@ struct IPAFileManager { // MARK: - Change bundle id and return eventual new filename - func changeBundleId(for file: LocalIPAFile, to newBundleId: String, overwriteFile: Bool) -> String? { + func changeBundleId(for file: LocalIPAFile, from oldBundleId: String, to newBundleId: String, overwriteFile: Bool) -> String? { let randomName = Global.randomString(length: 5) let tmp = documentsDirectoryURL().appendingPathComponent(randomName, isDirectory: true) @@ -229,9 +229,6 @@ struct IPAFileManager { } do { let ipaUrl = documentsDirectoryURL().appendingPathComponent(file.filename) - - debugLog(ipaUrl.absoluteString) - guard FileManager.default.fileExists(atPath: ipaUrl.path) else { return exit("IPA Not found") } if FileManager.default.fileExists(atPath: tmp.path) { try FileManager.default.removeItem(atPath: tmp.path) } try FileManager.default.createDirectory(atPath: tmp.path, withIntermediateDirectories: true) @@ -240,13 +237,24 @@ struct IPAFileManager { guard FileManager.default.fileExists(atPath: payload.path) else { return exit("IPA is missing Payload folder") } let contents = try FileManager.default.contentsOfDirectory(at: payload, includingPropertiesForKeys: nil) guard let dotApp = contents.first(where: { $0.pathExtension == "app" }) else { return exit("IPA is missing .app folder") } - let infoPlist = dotApp.appendingPathComponent("Info.plist", isDirectory: false) - guard FileManager.default.fileExists(atPath: infoPlist.path) else { return exit("IPA is missing Info.plist file") } - guard let dict = NSMutableDictionary(contentsOfFile: infoPlist.path) else { return exit("Unable to read contents of Info.plist file") } - - dict.setValue(newBundleId, forKey: "CFBundleIdentifier") - dict.write(to: infoPlist, atomically: true) + // Search recursively in .app folder for files named 'Info.plist' + let enumerator = FileManager.default.enumerator(atPath: dotApp.path) + if let filePaths = enumerator?.allObjects as? [String] { + let infoPlists = filePaths.filter { $0.contains("Info.plist") } + + // For each file found, replace old bundle id with new one + for plist in infoPlists { + let infoPlist = dotApp.appendingPathComponent(plist, isDirectory: false) + if FileManager.default.fileExists(atPath: infoPlist.path), let dict = NSMutableDictionary(contentsOfFile: infoPlist.path) { + if let oldValue = dict["CFBundleIdentifier"] as? String, oldValue.contains(oldBundleId) { + let newValue = oldValue.replacingOccurrences(of: oldBundleId, with: newBundleId) + dict.setValue(newValue, forKey: "CFBundleIdentifier") + dict.write(to: infoPlist, atomically: true) + } + } + } + } var destinationUrl: URL! diff --git a/appdb/Tabs/Downloads/Library/Library+Extension.swift b/appdb/Tabs/Downloads/Library/Library+Extension.swift index fc6c65ef..0813e4c3 100644 --- a/appdb/Tabs/Downloads/Library/Library+Extension.swift +++ b/appdb/Tabs/Downloads/Library/Library+Extension.swift @@ -140,7 +140,7 @@ extension Library { delay(0.2) { cell.updateText(ipa.size) - if let filename = IPAFileManager.shared.changeBundleId(for: ipa, to: newBundleId, overwriteFile: overwriteFile) { + if let filename = IPAFileManager.shared.changeBundleId(for: ipa, from: bundleId, to: newBundleId, overwriteFile: overwriteFile) { upload(filename: filename) } }