|
@@ -9,8 +9,12 @@ import Cocoa
|
|
|
import Foundation
|
|
|
import ZipArchive // 请确保已导入 SSZipArchive 或其他合适的解压库
|
|
|
|
|
|
+#if DEBUG
|
|
|
let xmlURLString = "http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.xml"
|
|
|
-let documentAIString = "http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.bundle.zip"
|
|
|
+#else
|
|
|
+let xmlURLString = "https://www.pdfreaderpro.com/downloads/DocumentAI.xml"
|
|
|
+#endif
|
|
|
+//let documentAIString = "http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.bundle.zip"
|
|
|
|
|
|
let kResourcePath = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?.path
|
|
|
|
|
@@ -60,6 +64,8 @@ class KMResourceDownloadManager: NSObject {
|
|
|
let exists = fileManager.fileExists(atPath: filePath as String)
|
|
|
if exists {
|
|
|
self.checkDocumentAIVersion()
|
|
|
+ } else {
|
|
|
+ self.removeXml()
|
|
|
}
|
|
|
return exists
|
|
|
}
|
|
@@ -86,10 +92,7 @@ class KMResourceDownloadManager: NSObject {
|
|
|
let filePath: String = kResourcePath! + "/DocumentAI.bundle"
|
|
|
try?FileManager.default.removeItem(atPath: filePath)
|
|
|
|
|
|
- let fileManager = FileManager.default
|
|
|
- let folderURL = fileManager.temporaryDirectory.appendingPathComponent("XMLResources")
|
|
|
- let xmlURL = folderURL.appendingPathComponent("DocumentAI.xml")
|
|
|
- try?FileManager.default.removeItem(at: xmlURL)
|
|
|
+ self.removeXml()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -127,11 +130,19 @@ extension KMResourceDownloadManager: XMLParserDelegate {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ func removeXml() {
|
|
|
+ let fileManager = FileManager.default
|
|
|
+ let folderURL = fileManager.temporaryDirectory.appendingPathComponent("XMLResources")
|
|
|
+ let xmlURL = folderURL.appendingPathComponent("DocumentAI.xml")
|
|
|
+ try?FileManager.default.removeItem(at: xmlURL)
|
|
|
+ }
|
|
|
+
|
|
|
func dealXML(content: String) -> String {
|
|
|
// 1. 定义 XML 内容,包括版本号
|
|
|
// let xmlContent = """
|
|
|
// <root>
|
|
|
-// <version>1.0</version>
|
|
|
+// <version>1.2.0</version>
|
|
|
+// <minVersion>1.4.0</minVersion> <!-- 最底支持版本参数,本地APP版本小于这个版本将不会更新 -->
|
|
|
// <resourceURL>http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.bundle.zip</resourceURL>
|
|
|
// </root>
|
|
|
// """
|
|
@@ -148,16 +159,26 @@ extension KMResourceDownloadManager: XMLParserDelegate {
|
|
|
|
|
|
// 4. 解析 XML 并比较版本号
|
|
|
// 创建 XMLParser 实例
|
|
|
- var downloadedVersion = "0.0"
|
|
|
+ var localVersion = "0.0"
|
|
|
+ var appVersion = "1.0.0"
|
|
|
+
|
|
|
if let xmlData = try? Data(contentsOf: self.fetchXMLURL()) {
|
|
|
let xmlParser = XMLParser(data: xmlData)
|
|
|
let xmlDelegate = XMLDelegate()
|
|
|
xmlParser.delegate = xmlDelegate
|
|
|
|
|
|
if xmlParser.parse() {
|
|
|
- downloadedVersion = xmlDelegate.version ?? "1.0"
|
|
|
+ localVersion = xmlDelegate.version ?? "1.0.0"
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // 从捆绑包的 Info.plist 文件中获取版本号
|
|
|
+ if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String {
|
|
|
+ appVersion = version
|
|
|
+ print("应用程序版本号:\(appVersion)")
|
|
|
+ } else {
|
|
|
+ print("无法获取应用程序版本号。")
|
|
|
+ }
|
|
|
|
|
|
if let xmlData = try? Data(contentsOf: xmlURL) {
|
|
|
let xmlParser = XMLParser(data: xmlData)
|
|
@@ -167,11 +188,17 @@ extension KMResourceDownloadManager: XMLParserDelegate {
|
|
|
if xmlParser.parse() {
|
|
|
// Parsing was successful
|
|
|
if let xmlVersion = xmlDelegate.version {
|
|
|
- if xmlVersion > downloadedVersion {
|
|
|
- // 下载资源的逻辑
|
|
|
- let resourceURL = xmlDelegate.resourceURL
|
|
|
- print("Download resource from \(resourceURL)")
|
|
|
- return resourceURL ?? ""
|
|
|
+ if xmlVersion > localVersion {
|
|
|
+ if let minVersion = xmlDelegate.minVersion {
|
|
|
+ if appVersion >= minVersion {
|
|
|
+ // 下载资源的逻辑
|
|
|
+ let resourceURL = xmlDelegate.resourceURL
|
|
|
+ print("Download resource from \(resourceURL)")
|
|
|
+ return resourceURL ?? ""
|
|
|
+ } else {
|
|
|
+ print("No need to download resources. Already up to date.")
|
|
|
+ }
|
|
|
+ }
|
|
|
} else {
|
|
|
print("No need to download resources. Already up to date.")
|
|
|
}
|
|
@@ -294,6 +321,7 @@ extension KMResourceDownloadManager: URLSessionDelegate, URLSessionDownloadDeleg
|
|
|
class XMLDelegate: NSObject, XMLParserDelegate {
|
|
|
var currentElement: String = ""
|
|
|
var version: String?
|
|
|
+ var minVersion: String?
|
|
|
var resourceURL: String?
|
|
|
|
|
|
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
|
|
@@ -309,6 +337,10 @@ class XMLDelegate: NSObject, XMLParserDelegate {
|
|
|
if resourceURL == nil {
|
|
|
resourceURL = string
|
|
|
}
|
|
|
+ } else if currentElement == "minVersion" {
|
|
|
+ if minVersion == nil {
|
|
|
+ minVersion = string
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|