How to create PLIST files programmatically in iphone
Asked Answered
E

3

15

I was looking to create plist file in my application Documents folder programmatically in objective C. I created a folder in documents directory :

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/Data.plist", documentsDirectoryPath];

I am trying to create plist file which will look like an XML file. /**** Required XML File ****/

<?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">
<array>
<dict>
    <key>height</key>
    <integer>4007</integer>
    <key>name</key>
    <string>map</string>
    <key>width</key>
    <integer>6008</integer>
</dict>
</array>
</plist>

/****Achieved file through code ****/

<?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>height</key>
    <string>4007</string>
    <key>name</key>
    <string>map</string>
    <key>width</key>
    <string>6008</string>
</dict>
</plist>

The required file needs an array and inside the array we have a dictionary object. How can I change this ? I also know how to write the file to the path, but the major issue is how to create plist file and then read it ?

Eanes answered 14/7, 2011 at 17:9 Comment(1)
I suggest you to refer this link: iphonesdevsdk.blogspot.com/2011/04/plist.html you can find your solution in best way.Mersey
P
68

A PLIST file, also known as a "Property List" file, uses the XML format to store objects such as arrays, dictionaries, and strings.

You can use this code for creating, adding the values and retrieving the values from the plist file.

//Get the documents directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) {

    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"plist.plist"] ];
}

NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: path]) {

    data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
}
else {
    // If the file doesn’t exist, create an empty dictionary
    data = [[NSMutableDictionary alloc] init];
}

//To insert the data into the plist
[data setObject:@"iPhone 6 Plus" forKey:@"value"];
[data writeToFile:path atomically:YES];

//To retrieve the data from the plist
NSMutableDictionary *savedValue = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSString *value = [savedValue objectForKey:@"value"];
NSLog(@"%@",value);
Pearl answered 14/7, 2011 at 17:26 Comment(4)
You have various variables defined twice like data. can you rectify it since I dont wanna debug the error?Eanes
It's very important to note only property-list-supported types will "just work" ( developer.apple.com/LIBRARY/IOS/#documentation/Cocoa/Conceptual/… ). All others must conform to the NSCoding protocol and be archived as an NSData object to be stored in the PLIST.Gentleness
@Kattupoochi : I have edited my question. Is it possible to add dictionary data within the array ?Eanes
Still the code contains redefined variables. @user08092013Pitsaw
M
4

I think this post save to .plist properity list will help you if you look through the examples there.

Also, check out Apple's Creating Property Lists Programmatically document for other guidelines and examples.

Murry answered 14/7, 2011 at 17:17 Comment(0)
C
3

Note that if you just want one plist file to keep a data, you don't really have to create and save any. There is a mechanism called NSUserDefaults. You do something like

[[NSUserDefaults standardUserDefaults] setInteger:1234 forKey:@"foo"];

and you read as in

NSInteger foo=[[NSUserDefaults standardUserDefaults] integerForKey:@"foo"];
// now foo is 1234

Preparing the file to save, writing it to the file, reading it again when the app is next launched, is done automatically for you!!

Read the official reference and the official document.

Cote answered 14/7, 2011 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.