美文网首页
钥匙串加密练习

钥匙串加密练习

作者: 永恒守护__刘鹏辉 | 来源:发表于2016-04-07 23:36 被阅读37次

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

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


    布局.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
    
    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
    
    SecondViewController.m截图.png

    相关文章

      网友评论

          本文标题:钥匙串加密练习

          本文链接:https://www.haomeiwen.com/subject/suhvlttx.html