我想在用户选择“记住我”复选框时保存密码。我有这样的代码:这是正确的吗?
在Appdelegate.m
@property(strong,nonatomic)NSString *savedno;
<。>文件中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.navcontrol =[[UINavigationController alloc]initWithRootViewController:self.viewController ];
self.window.rootViewController = self.navcontrol;
[self.window makeKeyAndVisible];
self.savedno=[[NSUserDefaults standardUserDefaults]objectForKey:@"pass"];
if(savedno==nil)
{
NSDictionary *saveDict=[NSDictionary dictionaryWithObject:savedno forKey:@"pass"];
[[NSUserDefaults standardUserDefaults]registerDefaults:saveDict];
}
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
[userdefault setObject:savedno forKey:@"pass"];
}
// ViewDelegate.m文件
//复选框事件
-(IBAction)check:(id)sender
{
if (checkboxSelected == 0)
{
[checkboxButton setSelected:YES];
[checkboxButton setImage:[UIImage imageNamed:@"checkbox-checked.png"]forState:UIControlStateNormal];
// userdefault=[NSUserDefaults standardUserDefaults];
// [userdefault setObject:user.text forKey:@"user"];
// [userdefault setObject:pass.text forKey:@"pass"];
self.callno=pass.text;
pass.text=self.callno;
AppDelegate *appdelegate=[[UIApplication sharedApplication]delegate];
appdelegate.savedno=self.callno;
NSLog(@"Data saved");
checkboxSelected = 1;
}
else
{
[checkboxButton setSelected:NO];
[checkboxButton setImage:[UIImage imageNamed:@"checkbox.png"]forState:UIControlStateNormal];
user.text=@"";
pass.text=@"";
checkboxSelected = 0;
}
}
- (void)viewDidLoad
{
user.clearButtonMode=UITextFieldViewModeWhileEditing;
AppDelegate *appdel=[[UIApplication sharedApplication]delegate];
if([user.text isEqualToString:@"bhoomi"])
{
pass.text=appdel.savedno;
}
[super viewDidLoad];
checkboxSelected = 0;
// Do any additional setup after loading the view, typically from a nib.
}
`
答案 0 :(得分:7)
别。 NSUserDefaults
不是密码等机密信息的地方。相反,请使用KeyChain。
答案 1 :(得分:3)
您应该使用钥匙串存储密码等信息。
查看SFHFKeychainUtils,它可以让您轻松使用应用中的钥匙串。它提供了两种方法:storeUserName:andPassword:和getPasswordForUsername,它们会使它变得简单,安全!
答案 2 :(得分:1)
要保存到用户默认值,请执行以下操作
[[NSUserDefaults standardUserDefaults] setValue:@"YourPassword" forKey:@"passowrd"];
//then call
[[NSUserDefaults standardUserDefaults] synchronize];
将其取回
NSString *password = [[NSUserDefaults standardUserDefaults] valueForKey:@"password"];
答案 3 :(得分:1)
将值设置为NSUserDefaults后,您必须同步一次才能保存它。使用以下语句来执行此操作。
[[NSUserDefaults standardUserDefaults] synchronize];
希望这有帮助。
修改强>
答案 4 :(得分:0)
使用此代码...
NSUserDefault *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setValue:pass_str forKey:@"pass"];
[userDefault synchronize];
这可能会对你有帮助......