How used last value of variable when application closed? - iphone

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];

Related

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 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...

iPhone - Populating an array with the content of compressed plist file

Let's say I have a plist file, and I want to compress it. I have a method that loads this compressed file, uncompress it, and put the result into a NSString.
How may I convert that NSString into an array as simple as it can be done with those lines when the plist is not compressed :
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"plist"];
NSArray* arrayOfDatas = [NSArray arrayWithContentsOfFile:filePath];
Read the file into an NSData then use:
+ (id)propertyListWithData:(NSData *)data options:(NSPropertyListReadOptions)opt format:(NSPropertyListFormat *)format error:(NSError **)error
where NSPropertyListFormat is NSPropertyListBinaryFormat_v1_0
Creating to NSData to write out is:
+ (NSData *)dataWithPropertyList:(id)plist format:(NSPropertyListFormat)format options:(NSPropertyListWriteOptions)opt error:(NSError **)error
Example (not tested):
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"plist"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSError *error;
NSArray* plist = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:&error];
NSData *propertyListSerializedData = [NSPropertyListSerialization dataWithPropertyList:plist format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
[propertyListSerializedData writeToFile:filePath atomically:YES];

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/

how to update data from web to exisitng file for iphone

NSString *applicationDocumentsDir =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:#"sample1.cfg"];
NSURL *instructionsURLd = [[NSURL alloc] initFileURLWithPath:filePath];
NSData *dataXML = [NSData dataWithContentsOfURL:instructionsURLd];
[dataXML writeToFile:storePath atomically:YES];
NSLog(#"matrix path %#",applicationDocumentsDir);
NSLog(#"neo path %#",storePath);
using this i can save data from my app to Documents folder right now sample1.cfg ia having 10 data now i want to add 10 more from web when user click on update button ?? so how to add 10 more data plz help
Thanks
...
NSData *dataXML = [NSData dataWithContentsOfURL:instructionsURLd];
NSMutableData *file = [[NSMutableData alloc] initWithContentsOfFile:storePath];
[file appendData:dataXML];
[file writeToFile:storePath atomically:YES];
...
This'll append the dataXML. If you want to prepend the data, use this:
...
NSMutableData *dataXML = [[NSData dataWithContentsOfURL:instructionsURLd] mutableCopy];
NSData *file = [[NSData alloc] initWithContentsOfFile:storePath];
[dataXML appendData:file];
[dataXML writeToFile:storePath atomically:YES];
...