Explorar o código

Swipe to delete keychain items

Tanner Bennett %!s(int64=6) %!d(string=hai) anos
pai
achega
37b5d1be2a

+ 30 - 14
Classes/GlobalStateExplorers/Keychain/FLEXKeychainTableViewController.m

@@ -14,7 +14,7 @@
 
 @interface FLEXKeychainTableViewController ()
 
-@property (nonatomic) NSArray<NSDictionary *> *keychainItems;
+@property (nonatomic) NSMutableArray<NSDictionary *> *keychainItems;
 @property (nonatomic) NSString *headerTitle;
 
 @end
@@ -40,7 +40,7 @@
 
 - (void)refreshkeychainItems
 {
-    self.keychainItems = [FLEXKeychain allAccounts];
+    self.keychainItems = [FLEXKeychain allAccounts].mutableCopy;
 }
 
 - (void)updateHeaderTitle
@@ -60,6 +60,24 @@
     return query;
 }
 
+- (void)deleteItem:(NSDictionary *)item
+{
+    NSError *error = nil;
+    BOOL success = [FLEXKeychain
+        deletePasswordForService:item[kFLEXKeychainWhereKey]
+        account:item[kFLEXKeychainAccountKey]
+        error:&error
+    ];
+
+    if (!success) {
+        [FLEXAlert makeAlert:^(FLEXAlert *make) {
+            make.title(@"Error Deleting Item");
+            make.message(error.localizedDescription);
+        } showFrom:self];
+    }
+}
+
+
 #pragma mark Buttons
 
 - (void)trashPressed
@@ -70,18 +88,7 @@
         make.message(@"This action cannot be undone. Are you sure?");
         make.button(@"Yes, clear the keychain").destructiveStyle().handler(^(NSArray *strings) {
             for (id account in self.keychainItems) {
-                FLEXKeychainQuery *query = [FLEXKeychainQuery new];
-                query.service = account[kFLEXKeychainWhereKey];
-                query.account = account[kFLEXKeychainAccountKey];
-
-                // Delete item or display error
-                NSError *error = nil;
-                if (![query deleteItem:&error]) {
-                    [FLEXAlert makeAlert:^(FLEXAlert *make) {
-                        make.title(@"Error Deleting Item");
-                        make.message(error.localizedDescription);
-                    } showFrom:self];
-                }
+                [self deleteItem:account];
             }
 
             [self refreshkeychainItems];
@@ -165,6 +172,15 @@
     return self.headerTitle;
 }
 
+- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)style forRowAtIndexPath:(NSIndexPath *)ip
+{
+    if (style == UITableViewCellEditingStyleDelete) {
+        [self deleteItem:self.keychainItems[ip.row]];
+        [self.keychainItems removeObjectAtIndex:ip.row];
+        [tv deleteRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationAutomatic];
+    }
+}
+
 
 #pragma mark - Table View Delegate