How To - Write/Read .plist files from an app

Discussion in 'iOS Development' started by gojohnnyboi, May 14, 2008.

  1. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Reading and writing to a .plist file is easy! making it a breeze to save application settings =) this is an example from my "Firmware.app"


    [OBJC]- (void)readPlist
    {
    NSString *filePath = @"/System/Library/CoreServices/SystemVersion.plist";
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

    NSString *value;
    value = [plistDict objectForKey

    Please Register or Log in to view images

    "ProductVersion"];

    /* You could now call the string "value" from somewhere to return the value of the string in the .plist specified, for the specified key. */
    }
    [/OBJC]

    [OBJC]
    - (void)writeToPlist
    {
    NSString *filePath = @"/System/Library/CoreServices/SystemVersion.plist";
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

    [plistDict setValue

    Please Register or Log in to view images

    "1.1.1" forKey

    Please Register or Log in to view images

    "ProductVersion"];
    [plistDict writeToFile:filePath atomically: YES];

    /* This would change the firmware version in the plist to 1.1.1 by initing the NSDictionary with the plist, then changing the value of the string in the key "ProductVersion" to what you specified */
    }

    [/OBJC]

    Hope this helps some people!
  2. God__

    God__ New Member

    Joined:
    Dec 23, 2007
    Messages:
    69
    Likes Received:
    0
    I love the consistency in your code that just proves that you copy and paste most of what you do. Look at the two variable declarations, NSString *variable and NSDictionary* variable1.

    It counts for something here.



    Anyway, regardless, if all you're trying to do is save your settings, I'm going to suggest that you look into NSUserDefaults. It's a lot easier than reading values (such as floats) and converting them.

    You also don't have to worry about where the file is saved or whether or not your application has the appropriate permissions to write to that directory. The OS will automaticalls save the settings in the user's Preferences directory. /var/userName/Library/Preferences/. That way your application can be used by more that one person.

    Also, if you are adding and removing settings, you're giong to want to use NSMutableDictionary anyway.




    EDIT: You should also add that writing any dictionary to a file will cause the default layout to be a property list formatted in XML. That way people who might read this don't try to create everyline of a file this way.

    Either way, thanks for covering the basics. I sure this might help someone.
  3. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    dude hells no i dont copy and paste. that dont even work in coding...


    and sorry i meant to put mutabledict. i have that in firmware.
  4. The Ax

    The Ax Banned

    Joined:
    Mar 11, 2008
    Messages:
    647
    Likes Received:
    22
    possible to do in JS?
  5. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55

    i think u can only change different parts in obj. in jigy u must compioletely rewrite the file
  6. tian_kr

    tian_kr New Member

    Joined:
    Mar 26, 2008
    Messages:
    935
    Likes Received:
    6

    what the heck. could you at least explain something?
    i doubt that anyone apart from some devs will understand a sh*t.
  7. nachos1010

    nachos1010 New Member

    Joined:
    May 7, 2008
    Messages:
    276
    Likes Received:
    0
    what is a .plist and what does it do?
  8. lgchicken

    lgchicken New Member

    Joined:
    Apr 12, 2008
    Messages:
    80
    Likes Received:
    1
    makes sense to me.

    i like gojohnnyboi's little bursts of knowledge, including his serial number thread.
    maybe there should be a wiki of useful tips and tricks. (or is there one already?)
  9. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55

    =p thank you! i'm actually working on something like that for my website @_@ shshhhhh
  10. ZunePod

    ZunePod Banned

    Joined:
    Aug 28, 2008
    Messages:
    1,340
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Would this work on something to go in the AppStore.

    What I mean is like where the path is defined, what would you change it to to stay in the App's Sandbox?

    And another thing, deleting from plists?

Share This Page