应该有一个包含全名,用户名,密码,电子邮件和注册按钮的注册页面。
当我们点击注册按钮时,我们转到登录页面,它使用NSUserDefaults
验证我们在注册页面中创建的用户名和密码。
现在,如果我们提供正确的凭据,那么它应该重定向到注销页面,当我们下次打开应用程序时,它应该直接重定向到注销页面。
数据应使用NSUserDefaults
在本地存储(值应以字符串格式存储)。
登录视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
@property(strong, nonatomic)NSString *dataString;
@property(strong, nonatomic)NSString *dataString2;
@property (strong, nonatomic) IBOutlet UILabel *lblOutput;
@property (strong, nonatomic) IBOutlet UITextField *inputTxt1;
@property (strong, nonatomic) IBOutlet UITextField *inputTxt2;
- (IBAction)btnAction:(id)sender;
@end
登录视图控制器.m
#import "ViewController2.h"
@interface ViewController2 ()
@end
@implementation ViewController2
- (void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firstName = [defaults objectForKey:@"firstName"];
NSString *lastName = [defaults objectForKey:@"lastname"];
firstName = _inputTxt1.text;
lastName = _inputTxt2.text;
[super viewDidLoad];
NSLog(@"%@",self.dataString);
NSLog(@"%@",self.dataString2);
self.navigationItem.title = @"Login Page";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnAction:(id)sender {
NSString *firstName = [_inputTxt1 text];
NSString *lastName = [_inputTxt2 text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:firstName forKey:@"firstName"];
[defaults setObject:lastName forKey:@"lastname"];
[defaults synchronize];
// [Defaults setObject:@"Eezy"forKey:@"iOS"];
NSLog(@"%@",[defaults stringForKey:@"firstName"]);
NSLog(@"%@",[defaults stringForKey:@"lastName"]);
if ([self.dataString isEqualToString:[defaults objectForKey:@"firstName"]] && [self.dataString2 isEqualToString:[defaults objectForKey:@"lastName"]])
{
NSLog(@"goog");
[self performSegueWithIdentifier:@"segueToNextPage2" sender:self];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Hey Listen" message:@"Wrong User Name or Password" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
@end
问题是我已经从注册页面传递了值,但是当我将它存储在NSUserDefault
中时,它存储了用户名的值,但是在密码的情况下,它显示为空值。
答案 0 :(得分:2)
保存登录值,您可以使用
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:txtUsername.text forKey:@"userName"];
[prefs setObject:txtPassword.text forKey:@"password"];
[prefs synchronize];
FOR RETRIEVING Values
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *savedUsername = [prefs stringForKey:@"userName"];
NSString *savedPassword = [prefs stringForKey:@"password"];
注销:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Your Key"];
答案 1 :(得分:2)
这是我得到的解决方案: -
步骤1:在登录页面中定义一个bool变量,并在单击登录按钮
时将其值设置为true- (IBAction)btnAction:(id)sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:@"registered"];
[prefs synchronize];
}
步骤2.在身份检查器中创建一个storyBoard作为“UITabBarController”。请记住,这将是您要去的页面。
步骤3.在@implemention下面的AppDelegate.h文件中编写以下代码: -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"registered"]) {
UITabBarController *controller = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"UITabBarController"];
self.window.rootViewController = controller;
}
return YES;
}
这很简单。
答案 2 :(得分:1)
将详细信息保存在注册页面的NSUserDefaults
中。同时在NSUserDefaults
中输入以检查用户是否登录。
[[NSUserDefaults standardUserDefaults]setValue:@"YES" forKey:@"isLoggedIn"];
在AppDelegate的didFinishLaunching
方法中,根据initialViewController
键值更改isLoggedIn
,例如
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"isLoggedIn"] isEqualToString:@"YES"])
{
// redirect to logout screen
}
else
{
//show login or register screen
}