Pages

Friday, 19 December 2014

How to create and manage login session in iOS






1] In login page , if Login is Successful then i stored my Username & Password in NSUserDefault
as follows
Onclick of Login Button.

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     [defaults setObject:userNameText forKey:@"username"];               
     [defaults setObject:passWordText forKey:@"password"];
     [defaults synchronize];
// Redirected to another viewController .


2] if user click Logout then  Delete All Data as follows.
Onclick of LogOut Button.
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"username"];
    [defaults removeObjectForKey:@"password"];
    [defaults synchronize];
 // Redirected to Main viewController or Login viewController .

And then

3] in case when user close app and  second time open app at that time , then we need to  check wheather username & password stored in NSUserDefault During ViewDidAppearof Loginpage.
ViewDidAppear of Loginpage.

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // check if user Login already
    if([defaults objectForKey:@"username"]!=nil  && ![[defaults objectForKey:@"username"] isEqualToString:@""])
    {
        // Redirected to viewController.
  

    }

No comments:

Post a Comment