SameSung Vs Iphone
SameSung Vs Iphone

Reputation: 614

Writing Textbox data to Plist

I have a textbox with submit button . I need when i press to submit and data in the textbox should be written in the plist . I tried the below code , but nothing is been changing in the plist .I have created a plist with name sample.plist.

 -(void) SubmitAction {
NSString *path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *finalPath = [path stringByAppendingPathComponent:@"sample.plist"];
NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
NSMutableArray *titleArray=[plistDict valueForKey:@"title"];

[titleArray addObject:textbox1.text];

[plistDict setValue:titleArray forKey:@"title"];

[plistDict writeToFile:finalPath atomically:NO];
}

The array created in the plist is below

<plist version="1.0">
<dict>
<key>title</key> <array/>
 </dict>
</plist>

please tell what else i need to do ..where is my fault

Upvotes: 1

Views: 213

Answers (5)

StackBuddy
StackBuddy

Reputation: 577

this worked for me my "Words.plist"

<dict>
    <key>Root</key>
    <array>
        <string>sunday</string>
        <string>monday</string>
        <integer>44</integer>
    </array>
</dict>


NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath];
NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"];
 _myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [_myDictionary objectForKey:@"Root"];
NSLog(@"%@", items);

Hope it helps :)

Upvotes: 0

Omar Freewan
Omar Freewan

Reputation: 2678

Try:
firstly to check if file exists

  bool b=[[NSFileManager defaultManager] fileExistsAtPath:filePath];
 if (!b) 
 {
      NSLog(@"The file does not exist");
      return;
 }

  ........

 [titleArray addObject:[NSString stringWithFormat:@"%@", textbox1.text]];

 [plistDict setObject:titleArray forKey:@"title"];

Now if the file does not exist follow apple documentation https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/CreatePropListProgram/CreatePropListProgram.html to create the file programmatically

Upvotes: 2

spider1983
spider1983

Reputation: 1098

You can use this working fine on my side

-(void)writeToPlist
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES); 
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"XYZ.plist"];

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];  
NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"XYZ.plist"];


if([fileManager fileExistsAtPath:documentPlistPath]){

    NSMutableDictionary *documentDict = [NSMutableDictionary 
    dictionaryWithContentsOfFile:documentPlistPath];
    NSMutableArray *valArray = [NSMutableArray arrayWithArray:[self readFromPlist]];
    int index = [valArray count];
    [valArray insertObject:@"lastObject" atIndex:index];
    [documentDict setObject:valArray forKey:@"title"];

    success =[documentDict writeToFile:documentPlistPath atomically:NO];

} else {

    NSError *error;
    BOOL written =  [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath 
    error:&error];

    if (written) {
        NSMutableDictionary *documentDict = [NSMutableDictionary 
        dictionaryWithContentsOfFile:documentPlistPath];
        NSMutableArray *valArray = [NSMutableArray arrayWithArray:[self readFromPlist]];
        int index = [valArray count];
        [valArray insertObject:@"lastObject" atIndex:index];
        [documentDict setObject:valArray forKey:@"title"];

        success =[documentDict writeToFile:documentPlistPath atomically:NO];

    }else {
        NSLog(@"Plist couldnot be copied from bundle to directory!!!");
    }

} 

}
 -(NSArray*)readFromPlist
 {
   NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  NSUserDomainMask, YES); 
  NSString *documentsDirectory = [documentPaths objectAtIndex:0];
 NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"XYZ.plist"];

   NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];

   NSArray *valueArray = [dict objectForKey:@"title"];

   return valueArray;

 }

replace @"lastobject" with your textbox.text;and replace XYZ.plist with your sample.plist.

Upvotes: 1

Paras Joshi
Paras Joshi

Reputation: 20551

try with this code..

NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"sample.plist"]) 
{
    if ([manager isWritableFileAtPath:plistPath]) 
    {
        NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
        NSMutableArray *titleArray=[plistDict valueForKey:@"title"];

        [titleArray addObject:textbox1.text];

        [plistDict setValue:titleArray forKey:@"title"];

        [plistDict writeToFile:finalPath atomically:NO];
        [manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]];
    }
}

i hope this help you...

Upvotes: 0

arthankamal
arthankamal

Reputation: 6413

try using,

[plistDict setObject:titleArray forKey:@"title"];

and check whether array is having value or nil object

Upvotes: 0

Related Questions