OSDN Git Service

Implement encrypt/decrypt.
[tombo/Tombo.git] / iOS / Tombo / Tombo / SinglePasswordDialog.m
1 #import "SinglePasswordDialog.h"
2
3 @implementation SinglePasswordDialog {
4     BOOL finished;
5 }
6
7 @synthesize password=_password;
8
9 - (id)initWithDefault {
10     return [self initWithTitle:@"Password" message:@"Please input password" delegate:self
11              cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
12 }
13
14 - (NSString *)showAndWait {
15     self.alertViewStyle = UIAlertViewStyleSecureTextInput;
16     [self show];
17     finished = NO;
18     
19     // Wait in runloop till a button is clicked.
20     while (!finished) {
21         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
22                                  beforeDate:[NSDate dateWithTimeIntervalSinceNow:300]];
23     }
24     return self.password;
25 }
26
27 #pragma mark - UIAlertViewDelegate
28
29 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
30     finished = YES;
31     if (buttonIndex == 0) {
32         // Cancel
33         self.password = nil;
34     } else if (buttonIndex == 1) {
35         // OK
36         UITextField *field = [self textFieldAtIndex:0];
37         NSString *pw = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
38         if (pw.length == 0) {
39             self.password = nil;
40         } else {
41             self.password = pw;
42         }
43     }
44 }
45
46 @end