iOS自动打包(重签名的方式)–用Mac客户端来实现
公司最近出了个需求,要求迅速给客户打一些马甲包,就是替换里面的plist和一些资源文件(icon和launchImage),于是找了很多资料,发现这一部分很多内容都已过期或者说讲的不全面,遂收集了一个全套的ipa重签名内容,分享给大家。代码是用swift写的,版本3.0
常量定义
struct PathDefine {
static let UnzipPath = NSTemporaryDirectory().appending("unzip")
static let TargetPath = "/Users/apple/Desktop"
static let outputPath : String = "/Users/apple/Desktop/autoPackage"
}
struct OtherStringDefine {
static let appid = "application-identifier"
static let kTeamIdentifier = "com.apple.developer.team-identifier"
static let kKeychainAccessGroups = "keychain-access-groups"
static let codeSignature = "_CodeSignature"
static let dbVersionKey = "dbVersion"
}
这些常量在以下内容中会用到
1解包
//解压之前先创建解压目录
fileprivate func createWorkingPath() {
let manager = FileManager.default
if manager.fileExists(atPath: PathDefine.UnzipPath) == false {
//不存在目录时创建一个
do {
try manager.createDirectory(atPath: PathDefine.UnzipPath, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error.localizedDescription)
}
} else {
// 存在目录时清空目录
do {
try manager.removeItem(atPath: PathDefine.UnzipPath)
} catch {
print(error.localizedDescription)
}
//再创建一个
do {
try manager.createDirectory(atPath: PathDefine.UnzipPath, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error.localizedDescription)
}
}
}
//这两个方法也是会多次被调用的,作用是监听task运行结果
fileprivate func checkComplete(task : Process, complete : @escaping (Bool) -> Void) {
Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timerComplete(sender:)), userInfo: ["task" : task, "complete" : complete], repeats: true)
}
@objc private func timerComplete(sender : Timer) {
let task : Process = (sender.userInfo as! Dictionary<String, Any>)["task"] as! Process
let complete : (Bool) -> Void = (sender.userInfo as! Dictionary<String, Any>)["complete"] as! ((Bool) -> Void)
if task.isRunning == false {
sender.invalidate()
if task.terminationStatus == 1 {
complete(false)
} else {
complete(true)
}
}
}
//开始解包
fileprivate func unzip(complete :@escaping (Bool) -> Void) {
let task = Process.init()
task.launchPath = "/usr/bin/unzip"
//这个record.ipaInputPath是我模型中的变量,ipa母包的绝对路径 大家可以把ipa丢到终端里面,就可以看到了
task.arguments = [ "-q", record.ipaInputPath, "-d", PathDefine.UnzipPath]
let pi = Pipe.init()
task.standardOutput = pi
let file = pi.fileHandleForReading
task.launch()
let data = file.readDataToEndOfFile()
print(String.init(data: data, encoding: .utf8) ?? "")
self.checkComplete(task: task) { [unowned self] (result) in
self.removeCodeSignature() //移除签名,内容在下面