[NSUserDefaults]的使用:登陆后不再显示登录界面。

简介:

NSUserDefaults是IOS应用用来存储用户偏好和配置信息的途径,就像是一个数据库,但是它通过键值对(key-value)的方式存储。

比如["Thematrix" forkey:"blogname"]

 

使用方法:

一共需要3个key,分别是"Didlogin" "username""userpassword"

1.在AppDelegate的LaunchOption函数里:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"Didlogin"]){
NSLog(@"未进行过登录,进行登录");
LoginViewController *LoginViewController =[storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
self.window.rootViewController = LoginViewController;
}
else
{
NSLog(@"已经进行过登录,直接到首页");
IndexViewController * IndexViewController = [storyboard instantiateViewControllerWithIdentifier:@"IndexViewController"];
self.window.rootViewController = IndexViewController;
}
return YES;
}
posted @ 2015-06-25 14:14  Dived  阅读(171)  评论(0编辑  收藏  举报