开发者

Data storing in plist works in simulaor but not in device

开发者 https://www.devze.com 2022-12-26 07:12 出处:网络
I am new to iPhone development. I have created plist such as from my previous post. It works well in simulator but not in device.

I am new to iPhone development. I have created plist such as from my previous post. It works well in simulator but not in device.

I am getting the saved value from the plist and checking for the condition. When I use simulator it w开发者_如何转开发orks but not in device.


(1) You can't write to a file in the resource folder of an iPhone. It is part of the security system of the phone that prevent malicious code from altering an application after it has been installed.

(2) If you want to save new data to a file you need to write the file to one of the automatically generated folders. Whenever an iPhone app is installed on the device or simulator, the system creates a default set of folders.

(3) You want to write to either the Preferences folder or the Documents folder. If the data concerns the operation of the app, write it to preferences. 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];

Say you want to read a default preference plist file, make some changes and then save it the preferences 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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号