|
@@ -23,11 +23,14 @@ class KMHeaderFooterManager: NSObject, NSCoding {
|
|
|
|
|
|
func reloadData() {
|
|
|
do {
|
|
|
+ self.fetchPlistData()
|
|
|
+
|
|
|
if let storedData = UserDefaults.standard.value(forKey: kHeaderFooterInfoSaveKey) as? Data {
|
|
|
NSKeyedUnarchiver.setClass(KMHeaderFooterObject.self, forClassName: "KMHeaderFooterObject")
|
|
|
NSKeyedUnarchiver.setClass(KMHeaderFooterManager.self, forClassName: "KMHeaderFooterManager")
|
|
|
if let man = NSKeyedUnarchiver.unarchiveObject(with: storedData) as? KMHeaderFooterManager {
|
|
|
self.headFooterObjects = man.headFooterObjects
|
|
|
+ self.savePlistData()
|
|
|
print(man)
|
|
|
}
|
|
|
}
|
|
@@ -145,3 +148,49 @@ class KMHeaderFooterManager: NSObject, NSCoding {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+let kHeaderFooterFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerFooter")
|
|
|
+let kHeaderFooterPlistPath = kHeaderFooterFolderPath?.stringByAppendingPathComponent("headerFooter.plist")
|
|
|
+let kHeaderFooterRemovedKey = "kHeaderFooterRemovedKey"
|
|
|
+
|
|
|
+extension KMHeaderFooterManager {
|
|
|
+ func fetchPlistData() {
|
|
|
+ if (!FileManager.default.fileExists(atPath: kHeaderFooterFolderPath!)) {
|
|
|
+ try?FileManager.default.createDirectory(atPath: kHeaderFooterFolderPath!, withIntermediateDirectories: false)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!FileManager.default.fileExists(atPath: kHeaderFooterPlistPath!)) {
|
|
|
+ FileManager.default.createFile(atPath: kHeaderFooterPlistPath!, contents: nil)
|
|
|
+ }
|
|
|
+ print("页眉页脚文件地址 =" + kHeaderFooterPlistPath!)
|
|
|
+
|
|
|
+ let plistData = try NSData(contentsOfFile: kHeaderFooterPlistPath!)
|
|
|
+ // 解档 plist 数据
|
|
|
+ if let headerFooterObject = try NSKeyedUnarchiver.unarchiveObject(with: plistData as! Data) as? KMHeaderFooterObject {
|
|
|
+ // 如果解档成功,可以使用 headerFooterObject
|
|
|
+ print("Header footer object: \(headerFooterObject)")
|
|
|
+ } else {
|
|
|
+ print("Failed to unarchive header footer object")
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ let dictionary = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ func savePlistData() {
|
|
|
+ // 获取 KMHeaderFooterObject 对象的归档数据
|
|
|
+ if let archivedData = try? NSKeyedArchiver.archivedData(withRootObject: headFooterObjects, requiringSecureCoding: false) {
|
|
|
+ // 将数据写入 plist 文件
|
|
|
+ let plistURL = URL(fileURLWithPath: kHeaderFooterPlistPath!)
|
|
|
+ do {
|
|
|
+ try archivedData.write(to: plistURL)
|
|
|
+ print("Header footer object saved to plist file")
|
|
|
+ } catch {
|
|
|
+ print("Error writing plist file: \(error)")
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ print("Failed to archive header footer object")
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|