iOS: NSUserDefaults issues

zz/2024/6/13 21:33:29

NSUserDefaults用于存储一些app setting。当你关闭app之后再打开后,之前存储的setting依然可以从NSUserDefaults里读取。注意:密码最好不要存储在NSUserDefaults里,要存储也必须用AES/DES加密存储,更安全的方法是 https://github.com/ldandersen/scifihifi-iphone

把密码存储在NSUserDefault or plist里的后果: http://www.shushao.com/news/info/item/203510-ios

example 1: 读取NSUserDefaults里的"color" setting

[[NSUserDefaults standardUserDefaults] stringForKey:@"color"];

example 2: 写入NSUserDefaults里的"color" setting为"red"

[[NSUserDefaults standardUserDefaults] setObject:@"red" forKey:@"color"];


另外NSUserDefaults还提供了一个registerDefaults方法,通常在appDelegate的didFinishLaunchingWithOptions里调用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{NSDictionary *defaultValues = [NSDictionary dictionaryWithObjectsAndKeys: @"blue", @"color",nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];return YES;
}


registerDefaults方法调用时会check NSUserDefaults里是否已经存在了相同的key,如果有则不会把其覆盖。举个例子:

1 当第一次run上面的app时,"color"="blue"会写入NSUserDefaults里

2 然后你的app调用了

[[NSUserDefaults standardUserDefaults] setObject:@"red" forKey:@"color"];

  这时NSUserDefaults里"color"="red"

3. exit app and restart it,registerDefaults方法还是会被调用,但它check到"color" key已经存在,就不会把它写入NSUserDefaults中

  这时NSUserDefaults里"color"="red"

4. 修改registerDefaults方法变成

    NSDictionary *defaultValues = [NSDictionary dictionaryWithObjectsAndKeys: @"black", @"color",nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];

  然后重新run app,之后NSUserDefaults里依然"color"="red"


5. 修改registerDefaults方法变成

NSDictionary *defaultValues = [NSDictionary dictionaryWithObjectsAndKeys: @"black", @"color",@"tomson", @"username",nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];

  然后重新run app,之后NSUserDefaults里依然"color"="red",不过多了a pair of key-value:  username=tomson


注意:

* If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your User Defaults might get saved

* 我没试过NSUserDefaults在exit app之后被保存,有人说碰到,并给出代码调用""的解决方案

ref links:

http://stackoverflow.com/questions/6033385/nsuserdefaults-not-being-saved-in-simulator

http://stackoverflow.com/questions/2622754/why-is-nsuserdefaults-not-saving-my-values



http://www.ngui.cc/zz/2731644.html

相关文章

Hibernate: cannot simultaneously fetch multiple bags 问题的解决办法

如果在一个pojo里使用超过一个List的变量,则会出现这个错误 详见: http://www.quanlei.com/2011/10/2817.html 他提供了4个解决方法,我采用方案3: 改变FetchMode为Fetch(FetchMode.SUBSELECT) 其他参考links: http://www.mkyong.…

iOS: UI 开发

中文 http://blog.csdn.net/column/details/zhf156.html Table View http://www.raywenderlich.com/312/beautiful-table-view-helper-class http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html http://cocoawithlove.com/2010/12/uitableview-co…

iOS: login window design

QuickDialog https://github.com/escoz/QuickDialog QQ登录界面demo http://www.cocoachina.com/bbs/read.php?tid69074&page1 Login interface design http://www.photoshop-plus.co.uk/2012/04/13/clean-mobile-login-screen-tutorial/ iPhone Modal View Smalle…

iOS: Classes for fetching and parsing XML or JSON via HTTP

http://cocoawithlove.com/2011/05/classes-for-fetching-and-parsing-xml-or.html

iOS: 2 Xib's with the same File's Owner for 1 IBOutlet object?

http://stackoverflow.com/questions/8978654/2-xibs-with-the-same-files-owner-for-1-iboutlet-object 若2个xib的file owner都指向同一个view controller,那么在2个xib不会同时被load的情况下,分别在不同xib上的控件可以指向view controller里的同一个…

Android: google play graphic assets guide

http://support.google.com/googleplay/android-developer/bin/answer.py?hlen&answer1078870

[iOS] ARC, @property 关键字

https://docs.google.com/document/d/1ZVV5RTzPMUreiFdAMws3jxi2i5Q49iClsGl6JQFwVGg/edit?uspsharing

Reset cert and provisioning profile for appwith push notification feature

参考文档: APNS tutorial: http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 之前写的apns笔记:http://blog.csdn.net/totogogo/article/details/7376214 注意:在xcode里开发带notification…

Logistic Regression详解

Regression是什么? Regression:找到一个模型去表示数据间的关系Linear Regression:使用线性模型拟合数据Logistic Regression:其实本质也是线性拟合,但引入了log,这个之后会详细说 也就是说,不…

c3p0配置

简介  C3P0是一个开源的JDBC 连接池&#xff0c;它实现了数据源和JNDI绑定&#xff0c;支持JDBC3规范和JDBC2的标准扩展。目前使用它的 开源项目有 Hibernate&#xff0c;Spring等。 C3P0配置  <c3p0-config> <default-config> <!--当 连接池中的连接耗…