I am new IOS to Development .I want to know about the Keychain process for storing the user name .My concept is ..if User is already logged in i need to show a password screen if not i need to show a user name screen to enter .
When i need to show the user name screen means ,in two scenarios
1)Not yet user logged in -First Time Login
2)Logged in and logout from the app by pressing logout button in password screen
This much of code only i did in app delegate ,here what i am doing is i am just storing the username in NSUserDefaults and if user name exists then i am showing second screen i.e., Password screen .
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *controller = (ViewController *)self.window.rootViewController;
NSUserDefaults *User = [NSUserDefaults standardUserDefaults];
NSString *userid = [User stringForKey:@"userName"];
if (userid != nil)
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// determine the initial view controller here and instantiate it with
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"PassVC"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
else
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// determine the initial view controller here and instantiate it with
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginVC"];
self.window.rootViewController = viewController;
//viewController.man
[self.window makeKeyAndVisible];
}
return YES;
}
Now , What i want is ,instead of storing userid in NSUser Defaults, i want to store userid in keychain .I don't have knowledge about the keychain .i searched in google but i didn't found anything clearly .Whether i need to include any frameworks for keychain ??Please anyone help me with the example code .Thanks in Advance !!!
Thanks of the reply .. i have created the keychain .when i tried to print the keychain object it is showing nil.i don't know why? this is my code..
This is my ViewController.h
#import <UIKit/UIKit.h>
#import "KeychainItemWrapper.h"
@interface ViewController : UIViewController {
KeychainItemWrapper *keyChain ;}
@property (weak, nonatomic) IBOutlet UIView *sub_View;
@property (weak, nonatomic) IBOutlet UITextField *UserId_txt;
@property (weak, nonatomic) IBOutlet UIButton *clickMe_btn;
- (IBAction)btn1:(id)sender;
@end
This is my View Controller .m
#import "ViewController.h"
#import "PasswordViewController.h"
#import "KeychainItemWrapper.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.UserId_txt.delegate = self;
NSString *userid = self.UserId_txt.text;
[keyChain setObject:userid forKey:@"user_id"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (IBAction)btn1:(id)sender {
NSString *user =[keyChain objectForKey:@"user_id"];
NSLog(@" the user id is :%@",user);
PasswordViewController *PassVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PassVC"];
[self.navigationController pushViewController:PassVC animated:YES];
}
@end