FLEXKeychainTableViewController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // FLEXKeychainTableViewController.m
  3. // FLEX
  4. //
  5. // Created by ray on 2019/8/17.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXKeychain.h"
  9. #import "FLEXKeychainQuery.h"
  10. #import "FLEXKeychainTableViewController.h"
  11. #import "FLEXUtility.h"
  12. @interface FLEXKeychainTableViewController ()
  13. @property (nonatomic) NSArray<NSDictionary *> *keyChainItems;
  14. @end
  15. @implementation FLEXKeychainTableViewController
  16. - (void)viewDidLoad
  17. {
  18. [super viewDidLoad];
  19. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(clearKeychain)];
  20. self.keyChainItems = [FLEXKeychain allAccounts];
  21. self.title = [NSString stringWithFormat:@"🔑 Keychain Items (%lu)", (unsigned long)self.keyChainItems.count];
  22. }
  23. - (void)clearKeychain
  24. {
  25. for (id account in self.keyChainItems) {
  26. FLEXKeychainQuery *query = [FLEXKeychainQuery new];
  27. query.service = account[kFLEXKeychainWhereKey];
  28. query.account = account[kFLEXKeychainAccountKey];
  29. if (![query deleteItem:nil]) {
  30. NSLog(@"Delete Keychin Item Failed.");
  31. }
  32. }
  33. self.keyChainItems = [FLEXKeychain allAccounts];
  34. [self.tableView reloadData];
  35. }
  36. #pragma mark - FLEXGlobalsEntry
  37. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row
  38. {
  39. return @"🔑 Keychain";
  40. }
  41. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row
  42. {
  43. return [self new];
  44. }
  45. #pragma mark - Table View Data Source
  46. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  47. {
  48. return self.keyChainItems.count;
  49. }
  50. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  51. {
  52. static NSString *CellIdentifier = @"Cell";
  53. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  54. if (!cell) {
  55. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  56. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  57. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  58. }
  59. NSDictionary *item = self.keyChainItems[indexPath.row];
  60. cell.textLabel.text = item[kFLEXKeychainAccountKey];
  61. return cell;
  62. }
  63. #pragma mark - Table View Delegate
  64. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  65. {
  66. NSDictionary *item = self.keyChainItems[indexPath.row];
  67. FLEXKeychainQuery *query = [FLEXKeychainQuery new];
  68. query.service = [item valueForKey:kFLEXKeychainWhereKey];
  69. query.account = [item valueForKey:kFLEXKeychainAccountKey];
  70. [query fetch:nil];
  71. NSString *msg = nil;
  72. if (query.password.length) {
  73. msg = query.password;
  74. } else if (query.passwordData.length) {
  75. msg = query.passwordData.description;
  76. } else {
  77. msg = @"No data";
  78. }
  79. UIAlertController *cv = [UIAlertController alertControllerWithTitle:@"Password" message:msg preferredStyle:UIAlertControllerStyleAlert];
  80. [cv addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  81. [cv dismissViewControllerAnimated:YES completion:nil];
  82. }]];
  83. [cv addAction:[UIAlertAction actionWithTitle:@"Copy" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  84. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  85. pasteboard.string = msg;
  86. }]];
  87. [self presentViewController:cv animated:YES completion:nil];
  88. }
  89. @end