Matthew Combatti
Matthew Combatti

Reputation: 1

Reading iOS Plist value from unknown dictionary key name?

I have the following PLIST and need to access the Assistant Identifier nested in the dictionary. The problem is that the first key under Accounts (begins with 4FD9E669...) is unique to each device. How can I return the Assistant Identifier without knowing the dictionary key?

Here is my 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>Accounts</key>
    <dict>
        <key>4FD9E669-A5EA-4526-A6D2-0440858221A6</key>
        <dict>
            <key>Assistant Identifier</key>
            <string>05483ADC-23E1-400E-83E8-F7365063F56F</string>
            <key>Last Assistant Data Anchor</key>
            <string>c1e3ad3c51d7fb962b29ca2deb4990c0f6ae8962</string>
            <key>Speech Identifier</key>
            <string>dcaf7c87-c023-456c-a200-7db8bd5e10f1</string>
            <key>Validation Expiration</key>
               <date>2012-07-17T22:37:37Z</date>
        </dict>
    </dict>
    <key>Session Language</key>
    <string>en-US</string>
</dict>
</plist>

I have tried the following which works if the A.Identifier is known...(which doesn't help)

NSString* filename = @"/var/mobile/Library/Preferences/com.apple.assistant.plist";
NSMutableDictionary* prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];

NSMutableDictionary* nestedPrefs = (NSMutableDictionary*)[prefs valueForKey:@"Accounts"];

NSMutableDictionary* aPrefs = (NSMutableDictionary*)[nestedPrefs valueForKey:@"4FD9E669-A5EA-4526-A6D2-0440858221A6"];

NSString* assistantPref = (NSString*)[aPrefs valueForKey:@"Assistant Identifier"];

UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Assistant Identifier" message:assistantPref  delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[theAlert show];
[theAlert release];

Please help! Thank You!

Upvotes: 0

Views: 670

Answers (1)

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

At first glance, you can just loop through all your keys until you get to the one you want.

for (NSString *key in [nestedPrefs allKeys]) {
    // Pick which key is the one you want
}

I don't think you need to do that. Here is logic to just skip the step of finding the right key.

for (NSMutableDictionary *aPrefs in [nestedPrefs allValues]) {
    NSString* assistantPref = [aPrefs valueForKey:@"Assistant Identifier"];
    if (assistantPref) {
       // Whatever you need.
    }
}

Hope that helps.

Upvotes: 1

Related Questions