Problem in Plist

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

  1. iphonejudy macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #1
    I want to write the data in the textfields(username and password) in to the plist.I wrote the below code ,But my Jsonapp.plist is not updated.

    Code:
    
    -(void)writeToList
    {
    	
    	NSLog(@"writeToList");
    	NSString *path = [[NSBundle mainBundle] bundlePath];
    	NSString *finalPath = [path stringByAppendingPathComponent:@"Jsonapp.plist"];
    	NSMutableDictionary  *plistData = [[NSMutableDictionary alloc ]initWithContentsOfFile:finalPath];
    	
    	NSString *username =txtUsername.text;
    	NSString *password =txtPassword.text;
    	NSLog(@"Username=%@",username);
    	[plistData setValue:username forKey:@"Username"];
    	[plistData writeToFile:finalPath atomically:YES];
        						   
    }
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Go and read the conceptual documents again: you should never, under any circumstances, be trying to write to a file in your apps bundle.
     
  3. iphonejudy thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #3
    Reading data is working fine with below code

    Code:
    -(void)readFromList
    {
    	
    	NSLog(@"readFromList");
    	NSString *path = [[NSBundle mainBundle] bundlePath];
    	NSString *finalPath = [path stringByAppendingPathComponent:@"Jsonapp.plist"];
    	NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
    	for (id key in plistData ) {
    		NSLog(@"bundle: key=%@, value=%@", key, [plistData objectForKey:key]);
    	}
    	
    	
    }
     
  4. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #4
    As robbieduncan said: read the documentation. Your apps bundle is write protected; you can only read from files in the apps bundle, not write.
     
  5. iphonejudy thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #5
    I tried with the below code.Still theplist is not updated.

    Code:
    the_username =[[NSString alloc] initWithString: txtUsername.text];
    	  the_password=[[NSString alloc] initWithString: txtPassword.text];	
    	NSString *error;
        NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Jsonapp.plist"];
        NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
    							   [NSArray arrayWithObjects: the_username, the_password, nil]
    														  forKeys:[NSArray arrayWithObjects: @"Username", @"Password", nil]];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
    																   format:NSPropertyListXMLFormat_v1_0
    														 errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
    		NSLog(@"Not written");
            [error release];
        }
    
    
     
  6. iphonejudy thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
  7. iphonejudy thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #7
    The below code is not working in the ipod.

    Code:
    - (void)writeToList
    {
    NSString *error;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Jsonapp.plist"];
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
    						   [NSArray arrayWithObjects: txtUsername.text, txtPassword.text, nil]
    													  forKeys:[NSArray arrayWithObjects: @"Username", @"Password", nil]];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
    															   format:NSPropertyListXMLFormat_v1_0
    													 errorDescription:&error];
    if(plistData) {
    	NSLog(@"Found");
    	[plistData writeToFile:plistPath atomically:YES];
    }
    else {
    	NSLog(error);
    	[error release];
    }
    }
    
    	
    
    
    -(void)readFromList
    {
           NSLog(@"init");
    		NSString *errorDesc = nil;
    		NSPropertyListFormat format;
    		NSString *plistPath;
    		NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
    														  NSUserDomainMask, YES) objectAtIndex:0];
    		plistPath = [rootPath stringByAppendingPathComponent:@"Jsonapp.plist"];
    		NSLog(@"plistPath.......");	
    		if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    	    NSLog(@"File Exist.......");
    			plistPath = [[NSBundle mainBundle] pathForResource:@"Jsonapp" ofType:@"plist"];
    			//plistPath = [rootPath stringByAppendingPathComponent:@"Jsonapp.plist"];
    
    			NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    			NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
    									  propertyListFromData:plistXML
    									  mutabilityOption:NSPropertyListMutableContainersAndLeaves
    									  format:&format
    									  errorDescription:&errorDesc];
    			if (!temp) {
    				NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    				}
    	
    	        if(edit == TRUE)
    			{	
    			txtUsername.text = [temp objectForKey:@"Username"];
    			txtPassword.text = [temp objectForKey:@"Password"];
    			}	
    			user = [temp objectForKey:@"Username"];
    			pass= [temp objectForKey:@"Password"];
    			NSLog(@"Username=%@",user);
    			NSLog(@"Password=%@",pass);
    		
    		}
    		
    		
    	
    }
    		
     
  8. xsmasher macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #8
    "Not working" in what way? The file isn't written, or the file isn't read? You should be able to set breakpoints and use the debugger to see what code path you're following and what the variable values are.

    I can see your readFromList method is still reading from the bundle:
    Code:
    //remove this line
    plistPath = [[NSBundle mainBundle] pathForResource:@"Jsonapp" ofType:@"plist"];
    
     

Share This Page