我需要在我的iPhone应用中实现“保持登录状态”或“记住我”复选框。我该如何实现?如何存储用户ID?
非常感谢帮助,
谢谢, VKS
答案 0 :(得分:5)
@VKS
1
-(void)autologin:(id)sender
{
ischecked =!ischecked;
UIButton *check = (UIButton*)sender;
if(ischecked == NO)
[check setImage:[UIImage imageNamed:@"checkbox1.png"] forState:UIControlStateNormal];
else
[check setImage:[UIImage imageNamed:@"checkbox-pressed.png"] forState:UIControlStateNormal];
}
2
if(ischecked == YES)
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:mainDelegate.uName forKey:@"username"];
[standardUserDefaults setObject:mainDelegate.uPassword forKey:@"password"];
[standardUserDefaults synchronize];
}
并检索密码/用户名
3
if(isChecked == YES)
{
NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
NSString *pass = [standard objectforkey:@"password"];
NSString *user = [standard objectforkey:@"username"];
}
if ([pass == nil] || [user == nil])
{
// Show the alert view that user is not valid.
}
else
{
// User is valid.
}
答案 1 :(得分:3)
由于NSUserDefaults不安全,我建议使用iOS密钥链持久保存密码。
看这里:http://iosdevelopertips.com/core-services/using-keychain-to-store-username-and-password.html
答案 2 :(得分:1)
检查复选框是否已选中,如果选中,则
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:mainDelegate.uName forKey:@"username"];
[standardUserDefaults setObject:mainDelegate.uPassword forKey:@"password"];
[standardUserDefaults synchronize];
这将保存您的变量,并在登录时检查已保存的NSUserDefaults。
if (standardUserDefaults) {
//Here check for the values if there are any values then use the same and login
}
检查谷歌上的任何NSUserDefaults教程,您将获得示例代码。