FLEXKeyChainTableViewController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. _keyChainItems = [FLEXKeychain allAccounts];
  21. self.title = [NSString stringWithFormat:@"🔑 KeyChains (%lu)", (unsigned long)self.keyChainItems.count];
  22. }
  23. - (void)clearKeyChain
  24. {
  25. for (id account in _keyChainItems) {
  26. FLEXKeychainQuery *query = [[FLEXKeychainQuery alloc] init];
  27. query.service = [account valueForKey:kSSKeychainWhereKey];
  28. query.account = [account valueForKey:kSSKeychainAccountKey];
  29. if(![query deleteItem:nil]) {
  30. NSLog(@"Delete Keychin Item Failed.");
  31. }
  32. }
  33. _keyChainItems = [FLEXKeychain allAccounts];
  34. [self.tableView reloadData];
  35. }
  36. #pragma mark - FLEXGlobalsEntry
  37. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row
  38. {
  39. return [NSString stringWithFormat:@"🔑 %@ KeyChain", [FLEXUtility applicationName]];
  40. }
  41. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row
  42. {
  43. FLEXKeyChainTableViewController *keyChainViewController = [self new];
  44. return keyChainViewController;
  45. }
  46. #pragma mark - Table View Data Source
  47. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  48. {
  49. return 1;
  50. }
  51. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  52. {
  53. return self.keyChainItems.count;
  54. }
  55. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  56. {
  57. static NSString *CellIdentifier = @"Cell";
  58. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  59. if (!cell) {
  60. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  61. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  62. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  63. }
  64. NSDictionary *item = self.keyChainItems[indexPath.row];
  65. cell.textLabel.text = item[kSSKeychainAccountKey];
  66. return cell;
  67. }
  68. #pragma mark - Table View Delegate
  69. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  70. {
  71. NSDictionary *item = self.keyChainItems[indexPath.row];
  72. FLEXKeychainQuery *query = [[FLEXKeychainQuery alloc] init];
  73. query.service = [item valueForKey:kSSKeychainWhereKey];
  74. query.account = [item valueForKey:kSSKeychainAccountKey];
  75. [query fetch:nil];
  76. NSString *msg = nil;
  77. if ([query.password length])
  78. {
  79. msg = query.password;
  80. }
  81. else if ([query.passwordData length])
  82. {
  83. msg = [query.passwordData description];
  84. }
  85. else
  86. {
  87. msg = @"NO Data!";
  88. }
  89. UIAlertController *cv = [UIAlertController alertControllerWithTitle:@"Password" message:msg preferredStyle:UIAlertControllerStyleAlert];
  90. [cv addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  91. [cv dismissViewControllerAnimated:YES completion:nil];
  92. }]];
  93. [cv addAction:[UIAlertAction actionWithTitle:@"Copy" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  94. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  95. pasteboard.string = msg;
  96. }]];
  97. [self presentViewController:cv animated:YES completion:nil];
  98. }
  99. @end