How to use the value in PSLinkListCell in preferen

2019-07-01 19:51发布

问题:

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:

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?

回答1:

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.