Search code examples
iosobjective-cdictionaryjailbreaktheos

How to read a plist dictionary, and save the keys (not the values) in array strings of another plist?


I´m creating a JB tweak with theos in my device.

I would like to read a plist dict like:

ASNPrefs.plist

    <?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>ASNcenterEnabled</key>
            <false/>
            <key>ASNcornerEnabled</key>
            <false/>
            <key>ASNnoCenterEnabled</key>
            <false/>
            <key>ASNdepthSizeEnabled</key>
            <false/>
        </dict>
        </plist>

and write only the keys as an array into another plist like:

ASNkeys.plist

    <?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>ASNkeys</key>
            <array>
                <string>ASNcenterEnabled</string>
                <string>ASNcornerEnabled</string>
                <string>ASNnoCenterEnabled</string>
                <string>ASNdepthSizeEnabled</string>
            </array>
        </dict>
    </plist>

I've created a button with this action, but nothing happens:

- DEFINED AT BEGINNING OF .MM:


#define prefs @"/User/Library/Preferences/ASNPrefs.plist"
#define asnKeysPath @"/User/Documents/asnKeys.plist"
#define asnKeysDict [[NSDictionary dictionaryWithContentsOfFile:asnKeysPath] objectForKey:@"keys"]



- IN THE IMPLEMENTATION:


   -(void)ASNkeys {
        NSDictionary *asnPrefs = [NSDictionary dictionaryWithContentsOfFile:prefs];
        NSArray *allKeys = [prefs allKeys];

    [allKeys writeToFile:asnKeysDict atomically:YES];
        }

Thank you!


Solution

  • Finally!!

    After lot of changes... Thank u so mach CRD!!

    NSString  *arrayPath;
    NSString  *dictPath;
    NSString  *origPath;
    
    // Get path to documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
                if ([paths count] > 0)  {
    
                    origPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"com.maat.asnPrefs.plist"];
    
                    NSDictionary *dictOrig = [NSDictionary dictionaryWithContentsOfFile:origPath];
    
                    NSArray *ASNKeys = [dictOrig allKeys]; //ForObject:@"true"];
    
                    NSDictionary *dictionary = @{@"ASNKeys" : allKeys};
    
                    // Path to save array data
                    arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"];
    
                    // Path to save dictionary
                    dictPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"asnKeys.plist"];
    
                    // Write array
                    [asnKeys writeToFile:arrayPath atomically:YES];
    
                    // Write dictionary
                    [dictionary writeToFile:dictPath atomically:YES];
    }