Can't write to a file on the device (but can in simulator)

Discussion in 'iOS Programming' started by dantastic, Feb 5, 2011.

  1. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #1
    I keep some settings in a plist.

    I've the following methods to open the plist and then save it again when I'm done.

    Code:
    		NSString *path = [[NSBundle mainBundle] bundlePath];
    		NSString *filePath = [path stringByAppendingPathComponent:@"MySettings.plist"];
    		MySettingsDictionary = [[NSMutableDictionary alloc] 
    							 initWithContentsOfFile:filePath];
    
    Code:
    - (void)save:(NSDictionary *)saveData {
    	NSString *path = [[NSBundle mainBundle] bundlePath];
    	NSString *filePath = [path stringByAppendingPathComponent:@"MySettings.plist"];
    	[saveData writeToFile:filePath atomically:YES];
    	NSLog(@"It this called at all??? - Yes it is...");
    }
    
    In simulator this works just fine, whatever I change in my settings gets written to the plist and I'm able to read the updated values from my main application then.

    Testing this on a physical iPhone yield a slightly different result - in that the file isn't updated. I dp get printed in the log "It this called at all??? - Yes it is..." but that's it, no other errors or anything.

    Anyone have any idea what the problem might be or how to troubleshoot this one?? I'm a bit stuck as it's working perfectly in the simulator...
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    IIRC, you are not allowed to modify your application's bundle in any way on the iPhone.
     
  3. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #3
    This.

    You need to write to a location in your app's sandbox (like it's Documents directory).
     
  4. dantastic thread starter macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011

Share This Page