Search code examples
iosobjective-cplisttheos

How To Update Plist File?


Okay I am trying update my tweaks bundles to the value of the NSMutableArray, valididentifiers which is populated with the users selected app identifiers such as com.apple.mobilesafari if the user selected the safari app inside the preference bundle for my tweak. However I am now trying to update the plist counting the supported bundles with the code bellow, however it only detects the Filter key when I wish to update to the Bundles key. I know it is only detecting the Filter as a valid key by logging bundlesplist allKeys attribute, and observing in the syslog.

So my question is how do I specifically update the Bundles section in the plist shown bellow?

test.plist file structure:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Filter</key>
        <dict>
        <key>Bundles</key>
        <array>
            <string>com.apple.mobilesafari</string>
            <string>com.apple.springboard</string>
        </array>
        </dict>
</dict>
</plist>

My Code:

NSMutableDictionary *bundlesplist = [NSMutableDictionary dictionaryWithContentsOfFile:@"/Library/MobileSubstrate/DynamicLibraries/test.plist"];
NSLog(@"%@", bundlesplist);
NSLog(@"%@", [bundlesplist allKeys]);

[bundlesplist setObject:valididentifiers forKey:@"Bundles"];
[bundlesplist writeToFile:@"/Library/MobileSubstrate/DynamicLibraries/test.plist" atomically:NO];

Syslog Output For allKeys

2017-07-18 20:25:28.627: (
    Filter
)

I am not sure if I am going about this the wrong way, or just being stupid but any help would be greatly appreciated. Many thanks Tom


Solution

  • Ideally, you should be able to set the value just using:

    [bundlesplist setObject:valididentifiers forKeyPath:@"Filter.Bundles"];
    

    However, the Filter dictionary is probably immutable.

    Therefore, I recommend to just do:

    NSMutableDictionary *filter = [bundlesplist[@"Filter"] mutableCopy];
    filter[@"Bundles"] = valididentifiers;
    bundlesplist[@"Filter"] = filter;