思路:
1.版本更新配置文件(plist,xml,或者txt能解析的格式文件),再制作一个dmg文件,放置在服务器上
2.每次启动APP更新配置文件,和本地版本相比较是否需要更新
3.如果需要更新,则配置全局变量shoudUpdate为true,否则为false
4.下载dmg文件
5.在需要更新的地方,触发打开dmg文件
6.弹出安装窗口后,退出当前应用
具体的代码如下:
import Cocoa
import Alamofire
let castPath = "http://xxxxx/cast.plist"
let dmgPath = "https://xxxxx/YourProjectName.dmg"
let castName = "cast.plist"
let dmgName = "YourProjectName.dmg"
let bashPath = "/bin/bash"
let tempDirectory = "Caches"
class APPUpdate: Any {
var shouldUpdate = false
static var shared: APPUpdate = {
var instance = APPUpdate()
return instance
}()
func downLoadCast() {
let destination : DownloadRequest.Destination = { _, response in
let library : String = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
var documentURL = URL(fileURLWithPath: library)
documentURL.appendPathComponent(tempDirectory)
let fileUrl = documentURL.appendingPathComponent(castName)
return (fileUrl , [.removePreviousFile, .createIntermediateDirectories])
}
AF.download(castPath, to: destination).downloadProgress { progress in
}.validate().responseData {response in
let library : String = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
var documentURL = URL(fileURLWithPath: library)
documentURL.appendPathComponent(tempDirectory)
let fileUrl = documentURL.appendingPathComponent(castName)
let fileManager = FileManager.default
if fileManager.fileExists(atPath:fileUrl.path) {
let dic : NSDictionary = NSDictionary(contentsOfFile:fileUrl.path)!
let version : String = dic["version"] as! String
let current = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
self.shouldUpdate = self.checkUdpate(version, location: current)
if self.shouldUpdate == true {
self.downLoadDmg()
}else{
do {
try fileManager.removeItem(atPath: fileUrl.path)
} catch let error as NSError {
print("删除YourProjectName.dmg失败 \(error.debugDescription)")
}
}
}
}
}
func checkUdpate(_ current:String,location:String) -> Bool {
var update = false
let clv = Array(location.split(separator: "."))
let csv = Array(current.split(separator: "."))
if clv.count != 3 || csv.count != 3 {
return update
}
for (i,item) in clv.enumerated() {
let update_item = csv[i]
let locationInt = Int(item)!
let updateInt = Int(update_item)!
if locationInt < updateInt {
update = true
break
}
}
return update
}
func doUpdate() {
let library : String = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
var documentURL = URL(fileURLWithPath: library)
documentURL.appendPathComponent(tempDirectory)
let result = doCmd(cmd: "cd \(documentURL.path);open \(dmgName)")
print("doCmd result : \(String(describing: result))")
}
func doCmd(cmd:String) -> String? {
let task = Process()
task.launchPath = bashPath
let arr = NSArray.init(objects: "-c",cmd)
task.arguments = arr as? [String]
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
let fileHandle = pipe.fileHandleForReading
task.launch()
let taskData = fileHandle.readDataToEndOfFile()
self.shouldUpdate = false
return NSString(data: taskData, encoding: String.Encoding.utf8.rawValue) as String?
}
func downLoadDmg() {
let destination : DownloadRequest.Destination = { _, response in
let library : String = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
var documentURL = URL(fileURLWithPath: library)
documentURL.appendPathComponent(tempDirectory)
let fileUrl = documentURL.appendingPathComponent(dmgName)
return (fileUrl , [.removePreviousFile, .createIntermediateDirectories])
}
AF.download(dmgPath, to: destination).downloadProgress { progress in
}.validate().responseData {response in
let library : String = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
var documentURL = URL(fileURLWithPath: library)
documentURL.appendPathComponent(tempDirectory)
let fileUrl = documentURL.appendingPathComponent(dmgName)
print("下载dmg完成 - \(fileUrl)")
}
}
}
使用代码示例:
1.判断是否需要更新
if (APPUpdate.shared.shouldUpdate == true) {
//your code
}
2.更新
APPUpdate.shared.doUpdate()
网友评论