Storing iPhone application settings in app

旧街凉风 提交于 2019-11-30 01:45:33

Best and easiest way to store settings in the iPhone is through NSUserDefaults. Keeps you from having to deal with the file system or plists or any of that other stuff.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *storedVal = @"This is what you want to save";
NSString *key = @"storedVal"; // the key for the data

[defaults setObject:storedVal forKey:key];
[defaults synchronize]; // this method is optional


// Get the results out
NSString *results = [defaults stringForKey:key];

Here's what Apple says on the types of objects you can store in the Defaults

A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

There are some more caveats, like if you store an NSDictionary the key values must be strings.

If you're looking for a UI to edit the settings from inside the app, check out InAppSettingsKit at http://www.inappsettingskit.com

Read the File and Networking Guide from the iPhone Developer Connection. It will explain how you get the path for the different predefined locations in the application sandbox. I'd recommend that you use a NSDictionary to store your preferences which can easily be saved to the file system and converted into a plist.

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