Some applications need to store information in order to be really valuable for the user. Others do it just for fun; for example, storing the highscores in a game. Yet others do it to offer a multi-platform experience, by making it possible to access the same information from several different platforms (web, desktop, mobile), where the iPhone might be one of them. In the last case, the information should probably be stored on a server on the Internet to make it really useful.
We can only get permission to write plist,if it is written in the document directory, on the run time the simulator also get accessible the document directory.So before writing to plist first we need to fetch the path of documents directory.
Step 1:Get the path of document directory.
-(NSString *)pathofFile {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsfolder = [paths objectAtIndex:0];
return [docsfolder stringByAppendingFormat:@"data.plist"];
}
Above method returns the path of document directory append with a "data.plist" file, it is our target plist file.
Step 2:Write to plist
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"100" forKey:@"mark"];
NSString *filepath = [self pathofFile];
[dict writeToFile:[self pathofFile] atomically:YES];
The above code write the value "100" to the key "mark" of data.plist file
Step 3:Read from Plist:
NSMutableDictionary *pListDict = nil;
NSString *filepath = [self pathofFile];
pListDict = [[NSMutableDictionary alloc]initWithContentsOfFile:filepath];
NSString *str = [pListDict valueForKey:@"mark"];
NSLog(str);
It print "100"
Hope it helps Some one.
Regards,
Companion.