Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iphonejudy

macrumors 6502
Original poster
Sep 12, 2008
301
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];
    						   
}
 
Go and read the conceptual documents again: you should never, under any circumstances, be trying to write to a file in your apps bundle.
 
Go and read the conceptual documents again: you should never, under any circumstances, be trying to write to a file in your apps bundle.

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]);
	}
	
	
}
 
As robbieduncan said: read the documentation. Your apps bundle is write protected; you can only read from files in the apps bundle, not write.
 
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];
    }
 
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);
		
		}
		
		
	
}
 
"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"];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.