3

I would like to make a preference bundle for my jailbreak tweak. I successfully made a preference bundle with a switch that works.

But I would like a setting to choose color. Example:

         Blue      
Color >  Yellow       
         Green

So if I click on Color, it would bring me to another screen which ask me to choose either "Blue", "Yellow" or "Green" I have looked at tutorials on the net, and I think this is a PSLinkList.

But I want my tweak to read the plist and record what color is chosen. I think it would read the validValues of the PSLinkList, am I right?

But what code would I use to read the plist?

EDIT: Nate helped me with adding the PSListcontroller but I met one problem:

enter image description here

Here is my plist http://pastebin.com/uNKzLBrf

<?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>items</key>
  <array>
    <dict>
      <key>cell</key>
      <string>PSGroupCell</string>
      <key>label</key>
      <string>Main</string>
    </dict>
    <dict>
      <key>cell</key>
      <string>PSSwitchCell</string>
      <key>default</key>
      <true/>
      <key>defaults</key>
      <string>com.junyi00.prefs</string>
      <key>key</key>
      <string>enabled</string>
      <key>label</key>
      <string>Enable</string>
    </dict>
    <dict>
      <key>cell</key>
      <string>PSGroupCell</string>
      <key>label</key>
      <string>Colors</string>
    </dict>
    <dict>
      <key>cell</key>
      <string>PSListLinkCell</string>
      <key>defaults</key>
      <string>com.junyi00.prefs</string>
      <key>detail</key>
      <string>PSListItemsController</string>
      <key>key</key>
      <string>color</string>
      <key>label</key>
      <string>Color</string>
      <key>validTitles</key>
      <array>
        <string>Default</string>
        <string>Random</string>
        <string>Yellow</string>
        <string>Blue</string>
        <string>Red</string>
        <string>Green</string>
      </array>
      <key>validValues</key>
      <array>
        <string>Default</string>
        <string>Random</string>
        <string>Yellow</string>
        <string>Blue</string>
        <string>Red</string>
        <string>Green</string>
      </array>
    </dict>
  </array>
  <key>title</key>
  <string>Flash Color</string>
</dict>
</plist>

Help me with this?

Nate
  • 31,017
  • 13
  • 83
  • 207
junyi00
  • 792
  • 3
  • 8
  • 28

1 Answers1

4

Take a look at this older tutorial. (Note: for others reading this, the tutorial does not use PreferenceLoader, which should now be used for this task)

In your case, I think the MyAppName.plist should contain something like this:

    <key>items</key>
    <array>
        <dict>
            <key>cell</key>
            <string>PSLinkListCell</string>
            <key>defaults</key>
            <string>com.mycompany.MyAppName</string>
            <key>detail</key>
            <string>PSListItemsController</string>
            <key>key</key>
            <string>color_pref</string>
            <key>label</key>
            <string>Color</string>
            <key>validTitles</key>
            <array>
                <string>Blue</string>
                <string>Yellow</string>
                <string>Green</string>
            </array>
            <key>validValues</key>
            <array>
                <string>Blue</string>
                <string>Yellow</string>
                <string>Green</string>
            </array>
        </dict>
    </array>

Which could then be read in by your code like this:

#define PLIST_FILENAME @"/var/mobile/Library/Preferences/com.mycompany.MyAppName.plist"
#define COLOR_PREF @"color_pref"

// an ivar
NSMutableDictionary* preferences;

- (void) initializePreferences {
    NSFileManager* fileManager = [NSFileManager defaultManager];

    // initialize the preferences
    if (![fileManager fileExistsAtPath: PLIST_FILENAME]) {

        // make sure the user settings have default values assigned
        NSDictionary* defaultPrefs = [[NSDictionary alloc] initWithObjectsAndKeys:
                                       @"Yellow", COLOR_PREF,
                                       nil];

        preferences = [[NSMutableDictionary alloc] initWithDictionary: defaultPrefs];
        [preferences writeToFile: PLIST_FILENAME atomically: YES];
    } else {
        preferences = [[NSMutableDictionary alloc] initWithContentsOfFile: PLIST_FILENAME];
    }
}

- (NSString*) colorPref {
    return [preferences valueForKey: COLOR_PREF];
}

I've skipped over some steps here, as it sounds like you're familiar with the general process of adding your jailbreak app, or tweak's, settings to Preferences.app. If this doesn't work for you, post a comment asking for clarification.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • Thanks a lot! I will try this soon. I saw that tut before but mainly what i was trying was to read the plist with my own code but failed. – junyi00 Mar 10 '13 at 16:08
  • I finally fixed my problem after a few tries using this, but Awkwardly, I am not able to choose what color(only the cell is showing, clicking it does nothing)Please help here – junyi00 Mar 15 '13 at 08:23
  • I added the plist in the Top Post – junyi00 Mar 16 '13 at 18:37
  • @junyi00, you misspelled `PSLinkListCell`. You typed `PSListLinkCell`. That's the problem. – Nate Mar 16 '13 at 20:57