Saving data in plist

Discussion in 'iOS Programming' started by iphonejudy, Jan 24, 2010.

  1. iphonejudy macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #1
    Reading and writing data in to plist r working fine.But when i run the app second time,I found that the datas r not saved.
    Code:
    -(void)readFromList
    {
    	
    	NSLog(@"readFromList");
    	
        NSString* plistPath = nil;
    	NSFileManager* manager = [NSFileManager defaultManager];
    	if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Jsonapp.plist"]) 
    	{
    	NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:plistPath] retain];	
    	for (id key in plistData ) {
    		user = [plistData objectForKey:@"Username"];
    		pass=[plistData objectForKey:@"Password"];
    		NSLog(@"user=%@",user);
    		NSLog(@"bundle: key=%@, value=%@", key, [plistData objectForKey:key]);
    	}
    	if(edit == TRUE)
    	{
    		
    	txtPassword.text=user;
    	txtUsername.text=pass;
    	}
    	}
    }
    
    
    -(void)writeToList
    {
    	NSLog(@"Write to plist");
    	NSString *error;
    NSString* plistPath = nil;
    NSFileManager* manager = [NSFileManager defaultManager];
    if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Jsonapp.plist"]) 
    {    NSLog(@"plistpath=%@",plistPath);
        if ([manager isWritableFileAtPath:plistPath]) 
        {
            NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
            [infoDict setValue:txtPassword.text forKey:@"Password"]; 
    		[infoDict setValue:txtUsername.text forKey:@"Username"];
    		[infoDict writeToFile:plistPath atomically:NO];
            [manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]];
            NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:infoDict
    																	   format:NSPropertyListXMLFormat_v1_0
    															 errorDescription:&error];
    	}
    }
     
  2. xsmasher macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #2
    You're writing to the bundle. You need to be writing your file to the NSDocumentDirectory instead. I'd go back to your other code that was using NSDocumentDirectory.
     
  3. iphonejudy thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #3
    The below code is write the data .


    Can anyone tell me reading the data from the plist?

    Code:
    - (void)writeToList 
    {
    	
    	NSLog(@"writeToList");
    	// Get path to settings file.
    	NSError *error = nil;
    	
    	// Get path to settings file.
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentsDirectory = [paths objectAtIndex:0];
    	NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Jsonapp.plist"]];
    	
    	// Get pointer to file manager.
    	NSFileManager *fileManager = [NSFileManager defaultManager];
    	
    	// If the file does not exist, copy the generic starter theme data file from the app bundle.
    	if(![fileManager fileExistsAtPath:filePath])
    	{
    		// Set path to the app bundle file.
    		NSString *appBundleFilePath = [[NSBundle mainBundle] pathForResource:@"Jsonapp" ofType:@"plist"];
    		
    		// Copy the app bundle file to the document directory.
    		[fileManager copyItemAtPath:appBundleFilePath toPath:filePath error:&error];
    	}
    	
    	// Load the settings dictionary with the contents of the settings file.
    	NSMutableDictionary *mDictData = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    	if (mDictData) {
    		NSLog(@"write file was found");
    		[mDictData setValue:txtPassword.text forKey:@"Password"]; 
    		[mDictData setValue:txtUsername.text forKey:@"Username"];
    		[mDictData writeToFile:filePath atomically: YES];
    		
    		
    	}
    	else {
    		NSLog(@"write file was NOT found");
    	}
    
    	
    }
    
    
     
  4. xsmasher macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #4
    You have the line to read the file:

    Code:
    NSMutableDictionary *mDictData = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    
    and the code to get the data out of the file:

    Code:
    user = [mDictData objectForKey:@"Username"];
    pass = [mDictData objectForKey:@"Password"];
    
    The code you posted to write the file will only work if there's a matching file included in the bundle - is that what you intend?
     

Share This Page