1

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

5 Answers5

2

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

Omar Freewan
  • 2,678
  • 4
  • 25
  • 49
1

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.

spider1983
  • 1,098
  • 1
  • 7
  • 14
  • Plist couldnot be copied from bundle to directory!! :(( – SameSung Vs Iphone Dec 06 '12 at 09:57
  • how are you making you r plist and where are you adding it coz this code is running good at my side...i guess you are making mistake at that point only. – spider1983 Dec 06 '12 at 09:59
  • is there any file with name sample.plist in your project ..if not then right click-> new file -> choose resource ->choose property list-> name it sample -> save, then click on sample.plist -> right click on the file add row name it title set it type as nsarray -> save it, run the above code. – spider1983 Dec 06 '12 at 10:04
  • ya please clear me.. if create my own plist? or it will be create automatically ?? i m not clear with this – SameSung Vs Iphone Dec 06 '12 at 10:05
  • no you will have to create one in your project with the all the tags and its type like i have commented ... – spider1983 Dec 06 '12 at 10:08
  • and don't forget to change the name of the plist file in my code with your plist file name i have edited my answer. check it. – spider1983 Dec 06 '12 at 10:12
  • not working :( i did same thing.. @lastobject = texbox1.text, sample.plist and plist with title – SameSung Vs Iphone Dec 06 '12 at 10:21
  • I tried other code which stores plist in document directory but everytime they overwrite the value.. I need it for REGISTER purpose ..so i should be saved – SameSung Vs Iphone Dec 06 '12 at 10:49
  • add NSLog(plist value %@,[self readFromPlist]);at end of -(void)writeToPlist method just to check, i guess if no nslog or error is coming its working . – spider1983 Dec 06 '12 at 10:50
  • @its working man!! but problem is its not updating my plist.plist is still the same...please check my plist code... – SameSung Vs Iphone Dec 06 '12 at 11:07
  • where are you looking your plist the updated plist file will be seen in documnet directory of your project not in your project plist. One thing you are not allowed to write in the file stored in the project resource/bundle so go to documnet directory and you can see the change. – spider1983 Dec 06 '12 at 11:13
  • are you getting the updated value in NSLog(plist value %@,[self readFromPlist]); if that is coming then its working fine. – spider1983 Dec 06 '12 at 11:20
  • ya man !! its updating !! actually i need it for RESGISTER and LOGIN LOGOUT purpose!! do you know how if i LOGIN how it will match with the plist? – SameSung Vs Iphone Dec 06 '12 at 11:23
  • 1
    take the value in NsmutableArray then match your string by iterating its index.If it matches the password is correct. – spider1983 Dec 06 '12 at 11:24
  • one more last thing if i need to take one more textbox ..what should i do..can you edit ur code one last time . I am trying to take ..arraywithobjects for objectforkey ...but code get crash – SameSung Vs Iphone Dec 06 '12 at 11:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20689/discussion-between-spider1983-and-samesung-vs-iphone) – spider1983 Dec 06 '12 at 11:41
  • Sircode u gave yesterday crashes.. check ..http://stackoverflow.com/questions/13744907/signin-check-credentials-from-plist – SameSung Vs Iphone Dec 07 '12 at 11:09
0

try using,

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

and check whether array is having value or nil object

arthankamal
  • 6,341
  • 4
  • 36
  • 51
0

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

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"sample.plist"] this path is which in if condition you tried it?? – Paras Joshi Dec 06 '12 at 10:27
  • where you get error dude?? and also try with your finalpath instead of plistpath in my code dude.. if any pro then tell me.. – Paras Joshi Dec 06 '12 at 10:51
0

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 :)

StackBuddy
  • 577
  • 5
  • 17