|
| 1 | +// |
| 2 | +// OTPListViewController.m |
| 3 | +// Coding_iOS |
| 4 | +// |
| 5 | +// Created by Ease on 15/7/2. |
| 6 | +// Copyright (c) 2015年 Coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "OTPListViewController.h" |
| 10 | +#import "ZXScanCodeViewController.h" |
| 11 | +#import "OTPTableViewCell.h" |
| 12 | + |
| 13 | +#import "OTPAuthURL.h" |
| 14 | + |
| 15 | +static NSString *const kOTPKeychainEntriesArray = @"OTPKeychainEntries"; |
| 16 | + |
| 17 | +@interface OTPListViewController ()<UITableViewDataSource, UITableViewDelegate> |
| 18 | + |
| 19 | +//Welcome |
| 20 | +@property (strong, nonatomic) UIImageView *tipImageView; |
| 21 | +@property (strong, nonatomic) UILabel *tipLabel; |
| 22 | +@property (strong, nonatomic) UIButton *beginButton; |
| 23 | + |
| 24 | +//Data List |
| 25 | +@property (strong, nonatomic) UITableView *myTableView; |
| 26 | + |
| 27 | +//Data |
| 28 | +@property (nonatomic, strong) NSMutableArray *authURLs; |
| 29 | + |
| 30 | +@end |
| 31 | + |
| 32 | +@implementation OTPListViewController |
| 33 | + |
| 34 | +- (void)viewDidLoad{ |
| 35 | + [super viewDidLoad]; |
| 36 | + self.title = @"身份验证器"; |
| 37 | + [self loadKeychainArray]; |
| 38 | +} |
| 39 | + |
| 40 | +- (void)loadKeychainArray{ |
| 41 | + NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; |
| 42 | + NSArray *savedKeychainReferences = [ud arrayForKey:kOTPKeychainEntriesArray]; |
| 43 | + self.authURLs = [NSMutableArray arrayWithCapacity:[savedKeychainReferences count]]; |
| 44 | + for (NSData *keychainRef in savedKeychainReferences) { |
| 45 | + OTPAuthURL *authURL = [OTPAuthURL authURLWithKeychainItemRef:keychainRef]; |
| 46 | + if (authURL) { |
| 47 | + [self.authURLs addObject:authURL]; |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +- (void)saveKeychainArray { |
| 53 | + NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; |
| 54 | + NSArray *keychainReferences = [self valueForKeyPath:@"authURLs.keychainItemRef"]; |
| 55 | + [ud setObject:keychainReferences forKey:kOTPKeychainEntriesArray]; |
| 56 | + [ud synchronize]; |
| 57 | +} |
| 58 | + |
| 59 | +- (void)viewWillAppear:(BOOL)animated{ |
| 60 | + [super viewWillAppear:animated]; |
| 61 | + [self configUI]; |
| 62 | +} |
| 63 | + |
| 64 | +#pragma mark p_M |
| 65 | + |
| 66 | +- (void)configUI{ |
| 67 | + if (self.authURLs.count > 0) { |
| 68 | + |
| 69 | + [self.tipImageView removeFromSuperview]; |
| 70 | + self.tipImageView = nil; |
| 71 | + |
| 72 | + [self.tipLabel removeFromSuperview]; |
| 73 | + self.tipLabel = nil; |
| 74 | + |
| 75 | + [self.beginButton removeFromSuperview]; |
| 76 | + self.beginButton = nil; |
| 77 | + |
| 78 | + if (!_myTableView) { |
| 79 | + // 添加myTableView |
| 80 | + _myTableView = ({ |
| 81 | + UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; |
| 82 | + tableView.backgroundColor = kColorTableSectionBg; |
| 83 | + tableView.dataSource = self; |
| 84 | + tableView.delegate = self; |
| 85 | + tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
| 86 | + [tableView registerClass:[TOTPTableViewCell class] forCellReuseIdentifier:NSStringFromClass([TOTPAuthURL class])]; |
| 87 | + [tableView registerClass:[HOTPTableViewCell class] forCellReuseIdentifier:NSStringFromClass([HOTPAuthURL class])]; |
| 88 | + [self.view addSubview:tableView]; |
| 89 | + [tableView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 90 | + make.edges.equalTo(self.view); |
| 91 | + }]; |
| 92 | + tableView; |
| 93 | + }); |
| 94 | + [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"addBtn_Nav"] style:UIBarButtonItemStylePlain target:self action:@selector(beginButtonClicked:)] animated:YES]; |
| 95 | + } |
| 96 | + [self.myTableView reloadData]; |
| 97 | + }else{ |
| 98 | + [self.navigationItem setRightBarButtonItem:nil animated:YES]; |
| 99 | + [self.myTableView removeFromSuperview]; |
| 100 | + self.myTableView = nil; |
| 101 | + |
| 102 | + if (!_tipImageView) { |
| 103 | + _tipImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tip_2FA"]]; |
| 104 | + [self.view addSubview:_tipImageView]; |
| 105 | + } |
| 106 | + if (!_tipLabel) { |
| 107 | + _tipLabel = [UILabel new]; |
| 108 | + _tipLabel.numberOfLines = 0; |
| 109 | + _tipLabel.textAlignment = NSTextAlignmentCenter; |
| 110 | + _tipLabel.font = [UIFont systemFontOfSize:16]; |
| 111 | + _tipLabel.textColor = [UIColor colorWithHexString:@"0x222222"]; |
| 112 | + _tipLabel.text = @"Coding 两步验证指的是用户登录账户的时候,除了要输入用户名和密码,还要求用户输入一个手机生成的动态密码,为帐户额外添加了一层保护,即使入侵者窃取了用户的密码,也会因不能使用用户的手机而无法登录帐户。"; |
| 113 | + [self.view addSubview:_tipLabel]; |
| 114 | + } |
| 115 | + if (!_beginButton) { |
| 116 | + _beginButton = [UIButton buttonWithStyle:StrapSuccessStyle andTitle:@"开始验证" andFrame:CGRectMake(kPaddingLeftWidth, CGRectGetHeight(self.view.frame)- 20 - 45, kScreen_Width-kPaddingLeftWidth*2, 45) target:self action:@selector(beginButtonClicked:)]; |
| 117 | + [self.view addSubview:_beginButton]; |
| 118 | + } |
| 119 | + [_tipImageView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 120 | + make.centerX.equalTo(self.view); |
| 121 | + make.top.mas_equalTo(kScreen_Height/6); |
| 122 | + }]; |
| 123 | + [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) { |
| 124 | + make.centerX.equalTo(self.view); |
| 125 | + make.top.equalTo(_tipImageView.mas_bottom).offset(40); |
| 126 | + make.left.equalTo(self.view).offset(kPaddingLeftWidth); |
| 127 | + make.right.equalTo(self.view).offset(-kPaddingLeftWidth); |
| 128 | + }]; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +- (void)beginButtonClicked:(id)sender{ |
| 133 | + __weak typeof(self) weakSelf = self; |
| 134 | + ZXScanCodeViewController *vc = [ZXScanCodeViewController new]; |
| 135 | + vc.sucessScanBlock = ^(OTPAuthURL *authURL){ |
| 136 | + [weakSelf addOneAuthURL:authURL]; |
| 137 | + }; |
| 138 | + [self.navigationController pushViewController:vc animated:YES]; |
| 139 | +} |
| 140 | + |
| 141 | +- (void)addOneAuthURL:(OTPAuthURL *)authURL{ |
| 142 | + [authURL saveToKeychain]; |
| 143 | + [self.authURLs addObject:authURL]; |
| 144 | + [self saveKeychainArray]; |
| 145 | + [self configUI]; |
| 146 | +} |
| 147 | + |
| 148 | +#pragma mark table_M |
| 149 | +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ |
| 150 | + return [self.authURLs count]; |
| 151 | +} |
| 152 | + |
| 153 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 154 | + return 1; |
| 155 | +} |
| 156 | + |
| 157 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 158 | + NSString *cellIdentifier = nil; |
| 159 | + OTPAuthURL *authURL = self.authURLs[indexPath.section]; |
| 160 | + if ([authURL isKindOfClass:[TOTPAuthURL class]]) { |
| 161 | + cellIdentifier = NSStringFromClass([TOTPAuthURL class]); |
| 162 | + }else{ |
| 163 | + cellIdentifier = NSStringFromClass([HOTPAuthURL class]); |
| 164 | + } |
| 165 | + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; |
| 166 | + [(OTPTableViewCell *)cell setAuthURL:authURL]; |
| 167 | + return cell; |
| 168 | +} |
| 169 | + |
| 170 | +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 171 | + return [OTPTableViewCell cellHeight]; |
| 172 | +} |
| 173 | + |
| 174 | +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ |
| 175 | + return kScaleFrom_iPhone5_Desgin(20); |
| 176 | +} |
| 177 | + |
| 178 | +- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ |
| 179 | + return 0.5; |
| 180 | +} |
| 181 | + |
| 182 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 183 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 184 | + OTPAuthURL *authURL = self.authURLs[indexPath.section]; |
| 185 | + if (authURL.otpCode.length > 0) { |
| 186 | + [[UIPasteboard generalPasteboard] setString:authURL.otpCode]; |
| 187 | + [self showHudTipStr:@"已复制"]; |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 192 | + return YES; |
| 193 | +} |
| 194 | + |
| 195 | +- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 196 | + if (editingStyle == UITableViewCellEditingStyleDelete) { |
| 197 | + OTPAuthURL *authURL = self.authURLs[indexPath.section]; |
| 198 | + [self.authURLs removeObject:authURL]; |
| 199 | + [authURL removeFromKeychain]; |
| 200 | + [self saveKeychainArray]; |
| 201 | + [self configUI]; |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +@end |
0 commit comments