read plist iphone sdk - iphone

I am trying to read a plist file using this -
NSData *data = [NSData dataWithContentsOfFile:SettingsFilePath];
NSPropertyListFormat format;
NSArray *array = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:nil];
but its not working.. is there any other way of doing this?

try with this way -
NSArray *arr= [[NSArray alloc] initWithContentsOfFile:plistPath];

Try + (id)arrayWithContentsOfFile:(NSString *)aPath to load it.

Related

how can I make offline browser for iOS application?

I need to make an offline browser on my iOS application, which allows the following:
When you have an internet connection (wifi, 3g, 4g, ...) you can download all the web pages you need to read during the day, then you can browse the content also when you have no internet connection.
I import the data from JSON file.
How can I do this?
If you have the data downloaded, save it to the documents directory and check if no internet connection, then load the content from the documents directory.
Get Documentpath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
If you have an NSDictionary:
NSString *path = [NSString stringWithFormat:#"%#",[paths objectAtIndex:0]];
NSDictionary *dict = [[NSDictionary alloc] init];
[dict writeToFile:[path stringByAppendingPathComponent:#"myOfflineData"] atomically:NO];
And to get it:
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:#"myOfflineData"]];
Or save it with a NSString etc.
To catch up your Example:
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http:......."]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; jsonResults = [jsonObjects objectForKey:#"nodes"];
[jsonData writeToFile:[path stringByAppendingPathComponent:#"myOfflineData"] atomically:NO];
And if you are offline:
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:#"myOfflineData"]];

How do .plist files work in iOS

I am new to iOS development, and I would like to know how .plist files work in iOS. Can any one help me by giving me an example of how to read an write data to and from a .plist?
Yup:
NSString *rootPath = [[NSBundle mainBundle] bundlePath];
NSString *pListPath = [rootPath stringByAppendingPathComponent:#"Settings.bundle/Root.plist"];
NSDictionary *pList = [NSDictionary dictionaryWithContentsOfFile:pListPath];
Writing to a file isn't too hard either:
NSDictionary *dict;
NSData *data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
[data writeToFile:self.saveToPath atomically:YES];

How used last value of variable when application closed?

I want to use an event like as i have some value in string and after storing value in string I have closed the application and now i again run that application. Now i want to use value of string which i save it in last time. How do that?
Thanks in advance...
you can use NSUserDefaults. you can refer following links
http://mobile.tutsplus.com/tutorials/iphone/nsuserdefaults_iphone-sdk/
http://www.cocoadev.com/index.pl?NSUserDefaults
http://www.icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/
[[NSUserDefaults standardUserDefaults] setObject:yourString forKey:yourKey]; // SET
yourString = [[NSUserDefaults standardUserDefaults] objectForKey:yourKey]; // GET
U should create a plist file .plist
step 1: #define DataFilePath before #implementation
#define DataFilePath [#"~/Documents/<fileName>.plist" stringByStandardizingPath]
step 2:create plist File than
if (![[NSFileManager defaultManager] fileExistsAtPath:DataFilePath])
{ NSData *data = [[NSData alloc] initWithContentsOfFile:[[NSBundle
mainBundle] pathForResource:#"" ofType:#"plist"]];
[data writeToFile:DataFilePath atomically:TRUE]; [data release]; } NSData *data = [NSData
dataWithContentsOfFile:DataFilePath]; NSLog(#"%#",DataFilePath);
NSPropertyListFormat format; NSArray *array =
[NSPropertyListSerialization propertyListFromData:data
mutabilityOption:NSPropertyListImmutable format:&format
errorDescription:nil];
step 3: save to plist file
NSString *path = [[CommonFunctions documentsDirectory]
stringByAppendingFormat:#"/%#",];
NSLog(#" file path = %#",path); [data writeToFile:path atomically:TRUE];

How to convert NSData to NSArray (or NSObject)

I did test this code, but it cause SIGABRT error.
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data]
NSData is plist data with xml format. This code works fine.
[urlData writeToFile:[self docPath] atomically:YES];
array = [[NSMutableArray alloc] initWithContentsOfFile:[self docPath]];
How can I change NSData to NSArray without file conversion?
This assumes you have populated NSString *filepath with the filepath of your saved data file.
NSPropertyListFormat format;
NSData *dataFromFile = [NSData dataWithContentsOfFile:fileNameWithPath];
NSArray *arrayFromFile = nil;
if (dataFromFile) {
arrayFromFile = [NSPropertyListSerialization propertyListFromData:dataFromFile
mutabilityOption:NSPropertyListMutableContainers
format:&format
errorDescription:NULL];
}
Hope that helps...

How can we create our own plist file in a Xcode project?

I want to create "UrlPaths.plist" file in my Application and also a dictionary with 4 or 5 objects. Please help me create a plist file and dictionary. And also read data from that plist file.
I want the plist to add the file to resources folder and i want to add Dictionary at that time also.i dont want pragmatical creation of plist but i want reading the data is pragmatically.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"plist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
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[#"value"] = #(5);
[data writeToFile: path atomically:YES];
[data release];
//To retrieve the data from the plist
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
int value1;
value1 = [savedStock[#"value"] intValue];
NSLog(#"%i",value1);
[savedStock release];
If you are about to create Plist without programmatically then follow these steps :
1. Right Click on Files in Left Pane and Select 'New File...' option.
2. Choose Resources from OS X tab.
3. An option for Property List is available.
4. Select an give an appropriate name.
This gets added to your project.
We can get simple understanding about plist as below
Now you can read this data as below
NSString *path = [[NSBundle mainBundle] pathForResource:#"Priority" ofType:#"plist"];
NSDictionary *dictPri = [NSDictionary dictionaryWithContentsOfFile:path];//mm
NSMutableArray *arrMarkets=[[NSMutableArray alloc] initWithArray:[dictPri valueForKey:#"List"]];
NSMutableArray *arrForTable=[[NSMutableArray alloc] init];
NSMutableArray *arrForTable1=[[NSMutableArray alloc] init];
for (NSDictionary *dict in arrMarkets)
{
NSString *strS1=nil;
strS1= [NSString stringWithFormat:#"%#",[dict valueForKey:#"Description"] ];
[arrForTable addObject:strS1];
}
NSLog(#"%#----------------- ",[arrForTable description]);
for (NSDictionary *dict in arrMarkets)
{
NSString *strS2=nil;
strS2= [NSString stringWithFormat:#"%#",[dict valueForKey:#"Name"] ];
[arrForTable1 addObject:strS2];
}
NSLog(#"%#----------------- ",[arrForTable1 description]);
create new plist file -
NSArray *Arr = [NSArray arrayWithObjects:obj1,obj2,nil];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:Arr format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
[data writeToFile:PlistDataFilePath atomically:YES];
Read data from this plist file -
NSData *data = [NSData dataWithContentsOfFile:PlistDataFilePath];
NSPropertyListFormat format;
NSArray *array = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:nil];
you should read this great tut on plist files - http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-use-plist-in-iphone/