1

There should be a registration page with Full Name, User Name, Password, Email and Register Button. When we Click on Register Button we then go to Login Page where it validate the username and password that we created in registration page using NSUserDefaults. Now if we give right credentials then it should redirect to logout page and when we next time open the app it should directly redirect to logout page. Data Should be stored locally using NSUserDefaults(value should be stored in string format).

Login view controller .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                                                                     

Login view controller .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      

The problem is that I have passed the value from registration page but when I am storing it in NSUserDefault then it is storing the value of username but in case of password it is showing null value.

UditS
  • 1,936
  • 17
  • 37
shubham mishra
  • 971
  • 1
  • 13
  • 32
  • 1
    http://stackoverflow.com/help/how-to-ask – Shubhank Apr 26 '16 at 07:51
  • 1
    Seems more like an assignment than a question. Have you tried anything or facing problems in any specific area? – UditS Apr 26 '16 at 07:52
  • //The problem is that have passed the value from registration page but when i am storing it in NSUserDefault then it is storing the value of username but in case of password it is showing error. – shubham mishra Apr 26 '16 at 07:55
  • 1
    You need to show at least code of what you tried, and point out your exact issue. And for sensitive info, you should use KeyChain. – Larme Apr 26 '16 at 07:56
  • Better way for storing username and password is using [Keychain](https://developer.apple.com/library/ios/documentation/Security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html#//apple_ref/doc/uid/TP30000897-CH208-SW1) . For a detailed answer refer to this [question](http://stackoverflow.com/questions/6972092/ios-how-to-store-username-password-within-an-app) One of the alternative to KeyChain [SSKeyChain](https://github.com/soffes/SSKeychain) – hariszaman Apr 26 '16 at 08:05

3 Answers3

2

FOR Saving login values you can use

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"];

For logout:

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Your Key"];
Abhinandan Pratap
  • 2,142
  • 1
  • 18
  • 39
2

Here is the Solution i got :-

Step 1: In login page define a bool variable and set its value as true on clicking the login button

- (IBAction)btnAction:(id)sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:@"registered"];
    [prefs synchronize];
}

Step 2. Create a storyBoard in identity inspector as "UITabBarController". Keep in mind this will be the page where you want to go.

Step 3. In AppDelegate.h file just below @implemention write following code :-

- (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;
}

That's it very simple.

shubham mishra
  • 971
  • 1
  • 13
  • 32
1

Save the details in NSUserDefaults from registration page. Also make entry in NSUserDefaults to check whether user logged in or not.

[[NSUserDefaults standardUserDefaults]setValue:@"YES" forKey:@"isLoggedIn"];

In your AppDelegate's didFinishLaunching method, change the initialViewController based on isLoggedIn key value e.g.

if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"isLoggedIn"] isEqualToString:@"YES"])
    {
        // redirect to logout screen
    }
    else
    {
        //show login or register screen
    }
Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49