conforming RemoteCommand class to NSSecureCoding

This commit is contained in:
True Zhuanjia committed 2024-07-24 20:09:16 +08:00
1 parent b6ecdcd0ea
commit cf21ca9503
1 file changed
+33 -9
+33 -9
View File
@@ -10,20 +10,30 @@ import Foundation
import UIKit import UIKit
// Define the RemoteCommand class // Define the RemoteCommand class
@objc public class RemoteCommand: NSObject, NSCoding { @objc public class RemoteCommand: NSObject, NSSecureCoding {
@objc public var keyboardCmdString: String // MARK: - NSSecureCoding
@objc public var alias: String
public static var supportsSecureCoding: Bool {
return true
}
// MARK: - Properties
@objc var keyboardCmdString: String
@objc var alias: String
// MARK: - Initialization
init(keyboardCmdString: String, alias: String) { init(keyboardCmdString: String, alias: String) {
self.keyboardCmdString = keyboardCmdString self.keyboardCmdString = keyboardCmdString
self.alias = alias self.alias = alias
} }
// MARK: - NSCoding // MARK: - NSSecureCoding
required public init?(coder: NSCoder) { required public init?(coder: NSCoder) {
guard let keyboardCmdString = coder.decodeObject(forKey: "keyboardCmdString") as? String, guard let keyboardCmdString = coder.decodeObject(of: NSString.self, forKey: "keyboardCmdString") as String?,
let alias = coder.decodeObject(forKey: "alias") as? String else { let alias = coder.decodeObject(of: NSString.self, forKey: "alias") as String? else {
return nil return nil
} }
self.keyboardCmdString = keyboardCmdString self.keyboardCmdString = keyboardCmdString
@@ -36,6 +46,7 @@ import UIKit
} }
} }
// Define the CommandManager class // Define the CommandManager class
@objc public class CommandManager: NSObject { @objc public class CommandManager: NSObject {
@objc public static let shared = CommandManager() @objc public static let shared = CommandManager()
@@ -114,12 +125,25 @@ import UIKit
} }
private func loadCommands() { private func loadCommands() {
if let savedCommandsData = UserDefaults.standard.data(forKey: "savedCommands"), if let savedCommandsData = UserDefaults.standard.data(forKey: "savedCommands") {
let savedCommands = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(savedCommandsData) as? [RemoteCommand] { do {
commands = savedCommands // Attempt to unarchive the data into an array of RemoteCommand
if let savedCommands = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, RemoteCommand.self], from: savedCommandsData) as? [RemoteCommand] {
// Assign the unarchived commands to your property
print(" Assign the unarchived commands to your property ")
commands = savedCommands
} else {
// Handle the case where the data could not be unarchived into the expected type
print("Data could not be unarchived into [RemoteCommand]")
}
} catch {
// Handle any errors that occur during unarchiving
print("Failed to unarchive savedCommands with error: \(error)")
}
} }
} }
private func saveCommands() { private func saveCommands() {
if let data = try? NSKeyedArchiver.archivedData(withRootObject: commands, requiringSecureCoding: false) { if let data = try? NSKeyedArchiver.archivedData(withRootObject: commands, requiringSecureCoding: false) {
UserDefaults.standard.set(data, forKey: "savedCommands") UserDefaults.standard.set(data, forKey: "savedCommands")