Data storing in plist works in simulaor but not in device

梦想的初衷 提交于 2019-12-11 00:05:05

问题


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 works but not in device.


回答1:


(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];


来源:https://stackoverflow.com/questions/2623750/data-storing-in-plist-works-in-simulaor-but-not-in-device

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!