@@ -120,32 +120,38 @@ struct ScriptPayloads: Codable {
120120// Utility to load `ScriptPayloads` from UserDefaults
121121class ScriptPayloadManager {
122122 private let userDefaults : UserDefaults
123+ private let appBundle : CFString = Bundle . main. bundleIdentifier! as CFString
123124
124125 init ( ) {
125126 self . userDefaults = UserDefaults . standard
126127 }
127128
128- // Loads and decodes ScriptPayloads from UserDefaults
129- func loadScriptPayloads( ) -> ScriptPayloads ? {
130- let forced = CFPreferencesAppValueIsForced ( " script_payloads " as CFString , Bundle . main. bundleIdentifier! as CFString )
131- guard let payloadDict = CFPreferencesCopyValue ( " script_payloads " as CFString , Bundle . main. bundleIdentifier! as CFString , kCFPreferencesAnyUser, kCFPreferencesAnyHost) else {
132- writeLog ( " No script payloads found in UserDefaults. " , logLevel: . debug)
133- return nil
129+ private var allPreferenceKeys : [ String ] {
130+ // Get the keys
131+ guard let keys = CFPreferencesCopyKeyList ( appBundle, kCFPreferencesAnyUser, kCFPreferencesAnyHost) as? [ String ] else {
132+ return [ ]
134133 }
134+ return keys
135+ }
135136
136- // if !forced {
137- do {
138- // Convert dictionary to Data and decode with PropertyListDecoder
139- let payloadData = try PropertyListSerialization . data ( fromPropertyList: payloadDict, format: . xml, options: 0 )
140- let decoder = PropertyListDecoder ( )
141- return try decoder. decode ( ScriptPayloads . self, from: payloadData)
142- } catch {
143- writeLog ( " Failed to decode script payloads: \( error) " , logLevel: . debug)
137+ // Loads and decodes ScriptPayloads from UserDefaults
138+ func loadScriptPayloads( ) -> ScriptPayloads ? {
139+ let forced = CFPreferencesAppValueIsForced ( " script_payloads " as CFString , appBundle)
140+ var currentPayload : ScriptPayloads = ScriptPayloads ( )
141+ for key in allPreferenceKeys where key. starts ( with: " script_payloads " ) {
142+ print ( " found key \( key) " )
143+ if let payloadDict = CFPreferencesCopyValue ( key as CFString , appBundle, kCFPreferencesAnyUser, kCFPreferencesAnyHost) {
144+ print ( payloadDict)
145+ do {
146+ let decoder = PropertyListDecoder ( )
147+ let payloadData = try PropertyListSerialization . data ( fromPropertyList: payloadDict, format: . xml, options: 0 )
148+ currentPayload = try decoder. decode ( ScriptPayloads . self, from: payloadData)
149+ } catch {
150+ writeLog ( " Failed to decode script payloads for \( key) : \( error) " , logLevel: . debug)
151+ }
144152 }
145- // } else {
146- // writeLog("Un-Managed payloads are not supported", logLevel: .debug)
147- // }
148- return nil
153+ }
154+ return currentPayload
149155 }
150156}
151157
@@ -155,3 +161,20 @@ func getScriptPayloads() -> ScriptPayloads {
155161 }
156162 return ScriptPayloads ( )
157163}
164+
165+ func appendContents( of propertyList: CFDictionary , to targetDictionary: inout ScriptPayloads ) {
166+ // Convert CFPropertyList to Swift Dictionary
167+ guard let plistDictionary = propertyList as? [ String : Any ] else {
168+ print ( " Error: CFPropertyList is not a dictionary " )
169+ return
170+ }
171+
172+ // Append each key-value pair into the target dictionary
173+ for (key, value) in plistDictionary {
174+
175+ }
176+ }
177+
178+ public func + < K, V> ( left: [ K : V ] , right: [ K : V ] ) -> [ K : V ] {
179+ return left. merging ( right) { $1 }
180+ }
0 commit comments