Saving data to plist works in simulaor but not device
1 answer
(1) You cannot write the file to the iPhone resource folder. This is part of the phone's security system that prevents malicious code from modifying the application after it has been installed.
(2) If you want to save the new data to a file, you need to write the file to one of the auto-generated folders. Whenever an iPhone app is installed on a device or simulator, the system creates a set of default folders.
(3) You want to write to either the Settings folder or the Documents folder. If the data relate to the operation of the application, write it in the settings. If it contains user data, write to "Documents".
Preferences:
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES);
NSString *prefsDirectory = [[sysPaths objectAtIndex:0] stringByAppendingPathComponent:@"/Preferences"];
Documents:
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
NSString *docsDirectory = [sysPaths objectAtIndex:0];
Let's say you want to read the default pland pluet file, make some changes and then save the settings folder.
NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"PlistFileName" ofType:@"plist"];
NSMutableArray *defaultPrefs=[[NSMutableArray alloc] initWithContentsOfFile:plistPath];
//... modify defaultPrefs
NSString *outputFilePath=[prefsDirectory stringByAppendingPathComponent:@"alteredPrefs.plist"];
[defaultPrefs writeToFile:outputFilePath atomically:NO];
+2
source to share