I am creating a iPhone app in which i get all countries name, logo & player name. I want to save that data in .plist
instead of sqlite
server. I don't know how to create a plist file in DocumentDirectory
and save the data.
Please somebody suggest me how to save data in plist file.
I am going through with screenshot and step by step. Please follow this and you will get your answer.
First you have to create Property List through your Xcode.
Step:1
Step:2
Step:3
Save data on your save button action :
// Take 3 array for save the data .....
-(IBAction)save_Action:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
[self.nameArr addObject:self.nameField.text];
[self.countryArr addObject:self.countryField.text];
[self.imageArr addObject:@"image.png"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.nameArr, self.countryArr, self.imageArr, nil] forKeys:[NSArray arrayWithObjects: @"Name", @"Country",@"Image", nil]];
NSError *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
alertLbl.text = @"Data saved sucessfully";
}
else
{
alertLbl.text = @"Data not saved";
}
}
// Data is saved in your plist and plist is saved in DocumentDirectory
Step:4
Retrieve Data from plist File:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.nameArr = [dict objectForKey:@"Name"];
self.countryArr = [dict objectForKey:@"Country"];
Step:5
Remove data from plist file:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];
self.nameArr = [dictionary objectForKey:@"Name"];
self.countryArr = [dictionary objectForKey:@"Country"];
[self.nameArr removeObjectAtIndex:indexPath.row];
[self.countryArr removeObjectAtIndex:indexPath.row];
[dictionary writeToFile:plistPath atomically:YES];
Step:6
Update your data on Update click Action:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"];
}
self.plistDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
[[self.plistDic objectForKey:@"Name"] removeObjectAtIndex:self.indexPath];
[[self.plistDic objectForKey:@"Country"] removeObjectAtIndex:self.indexPath];
[[self.plistDic objectForKey:@"Image"] removeObjectAtIndex:self.indexPath];
[[self.plistDic objectForKey:@"Name"] insertObject:nameField.text atIndex:self.indexPath];
[[self.plistDic objectForKey:@"Country"] insertObject:countryField.text atIndex:self.indexPath];
[[self.plistDic objectForKey:@"Image"] insertObject:@"dhoni.jpg" atIndex:self.indexPath];
[self.plistDic writeToFile:plistPath atomically:YES];
SWIFT 3.0
Below is the code to read and write Data in .plist File.
Create a data.plist file.
Make sure that root object is of type Dictionary.
class PersistanceViewControllerA: UIViewController {
@IBOutlet weak var nationTextField: UITextField!
@IBOutlet weak var capitalTextField: UITextField!
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
displayNationAndCapitalCityNames()
//Get Path
func getPath() -> String {
let plistFileName = "data.plist"
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentPath = paths[0] as NSString
let plistPath = documentPath.appendingPathComponent(plistFileName)
return plistPath
}
//Display Nation and Capital
func displayNationAndCapitalCityNames() {
let plistPath = self.getPath()
self.textView.text = ""
if FileManager.default.fileExists(atPath: plistPath) {
if let nationAndCapitalCities = NSMutableDictionary(contentsOfFile: plistPath) {
for (_, element) in nationAndCapitalCities.enumerated() {
self.textView.text = self.textView.text + "\(element.key) --> \(element.value) \n"
}
}
}
}
//On Click OF Submit
@IBAction func onSubmit(_ sender: UIButton) {
let plistPath = self.getPath()
if FileManager.default.fileExists(atPath: plistPath) {
let nationAndCapitalCities = NSMutableDictionary(contentsOfFile: plistPath)!
nationAndCapitalCities.setValue(capitalTextField.text!, forKey: nationTextField.text!)
nationAndCapitalCities.write(toFile: plistPath, atomically: true)
}
nationTextField.text = ""
capitalTextField.text = ""
displayNationAndCapitalCityNames()
}
}
output:
<?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>Canada</key>
<string>Ottawa</string>
<key>China</key>
<string>Beijin</string>
<key>Germany</key>
<string>Berlin</string>
<key>United Kingdom</key>
<string>London</string>
<key>United States of America</key>
<string>Washington, D.C.</string>
</dict>
</plist>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With