Write to .plist works on simulator, not on device

Discussion in 'iOS Programming' started by ryMac, Jun 15, 2009.

  1. ryMac macrumors newbie

    Joined:
    Jun 11, 2009
    #1
    I am trying to store a single an email address for an application in the plist file. It works fine on the simulator, but it doesn't work on my iPhone. Here is my code:

    code to view what is in the .plist
    Code:
    //code to retrieve info from plist
    	NSString *path = [[NSBundle mainBundle] bundlePath];
    	NSString *finalPath = [path stringByAppendingPathComponent:@"Info.plist"];
    	NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
    	
    	NSString *emailString = [NSString stringWithFormat:@"%@", [plistData objectForKey:@"savedEmail"]];
    	fromEmailInput.text = emailString;
    	NSLog(emailString);
    
    code to write to the .plist
    Code:
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *propList = [path stringByAppendingPathComponent:@"Info.plist"];
    	
    NSMutableDictionary* plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:propList];
    if (plistData) {
    	NSLog(@"write file was found");
    	[plistData setValue:fromEmailInput.text forKey:@"savedEmail"];
    	[plistData writeToFile:propList atomically: YES];
    }
    
    "write file was found" shows properly in the log when I run the app on my phone. Any idea why this appears to write properly on the simulator and not the phone? Anyone else run into this?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What path are you trying to write to? You can only write to your applications sandboxed area of the filesystem on the device (and certainly not into your applications bundle).
     
  3. hell1197 macrumors newbie

    Joined:
    Jun 15, 2009
  4. dusker macrumors member

    Joined:
    May 31, 2009
    #4
    If mainbundle is readonly then to solve this problem we should create another bundle right? can anyone share some code snippet?
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    No. You should not create a bundle at all. You should write to your applications sandboxed area on the filesystem as clearly described in the documentation.

    Seriously it's not rocket science. If you have not read all of the iPhone Application Programming Guide stop writing code right now and read it. It tells you how to do pretty much every basic operation.
     
  6. ryMac thread starter macrumors newbie

    Joined:
    Jun 11, 2009
    #6
    I ended up just writing my value to NSUserDefaults. That worked fine.

    Here is the code for that:

    Code:
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:fromEmailInput.text forKey:@"savedEmail"];
    Thanks for responding.
     

Share This Page