钥匙串加密练习

有两个页面,第一个页面上有2个TextField和1个Button,第二个页面有两个label
要求实现:点击Button按钮时第一个页面跳转到第二个页面,同时TextField中输入的值可以显示在label上

方法:在Main.storyboard中完成布局,拖动两个ViewController和一个Navigation Controller,在ViewController中拖进去相应的控件,实现页面跳转,在Button上添加方法


钥匙串加密练习_第1张图片
布局.png

----------------ViewController.m-------------------

#import "ViewController.h"

#import "KeychainItemWrapper.h"

#import "SecondViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UITextField *textField1;

@property (strong, nonatomic) IBOutlet UITextField *textField2;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (IBAction)button:(id)sender
{
    //创建一个钥匙串对象
    // 参数一 : 表示这个钥匙串对象的标识符
    // 参数二 : 分组, 一般为 nil
    KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyItemWrapper" accessGroup:nil];
    
    id kUserName = (__bridge id)kSecAttrAccount;
    id kPassWord = (__bridge id)kSecValueData;
    
    [wrapper setObject:self.textField1.text forKey:kUserName];
    [wrapper setObject:self.textField2.text forKey:kPassWord];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
钥匙串加密练习_第2张图片
ViewController.m截图.png

-------------SecondViewController.m----------------

#import "SecondViewController.h"

#import "KeychainItemWrapper.h"

@interface SecondViewController ()

@property (strong, nonatomic) IBOutlet UILabel *label1;

@property (strong, nonatomic) IBOutlet UILabel *label2;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blueColor];
    
    KeychainItemWrapper *newWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyItemWrapper" accessGroup:nil];
    
    id kUserName = (__bridge id)kSecAttrAccount;
    id kPassWord = (__bridge id)kSecValueData;
    
    self.label1.text = [newWrapper objectForKey:kUserName];
    self.label2.text = [newWrapper objectForKey:kPassWord];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
钥匙串加密练习_第3张图片
SecondViewController.m截图.png

你可能感兴趣的:(钥匙串加密练习)