Repository navigation
Expand file tree
/
Copy pathHAVerificationViewController.m
More file actions
121 lines (104 loc) · 4.27 KB
/
Copy pathHAVerificationViewController.m
File metadata and controls
121 lines (104 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#import "HAVerificationViewController.h"
@interface HAVerificationViewController ()
@property(nonatomic, copy) NSString *verificationMessage;
@property(nonatomic, retain) UILabel *messageLabel;
@property(nonatomic, retain) UITextField *codeField;
@property(nonatomic, retain) UIButton *verifyButton;
@property(nonatomic, assign) BOOL submitting;
@end
@implementation HAVerificationViewController
@synthesize delegate = _delegate;
@synthesize verificationMessage = _verificationMessage;
@synthesize messageLabel = _messageLabel;
@synthesize codeField = _codeField;
@synthesize verifyButton = _verifyButton;
@synthesize submitting = _submitting;
- (id)initWithMessage:(NSString *)message {
self = [super init];
if (self) {
self.verificationMessage = message;
self.modalPresentationStyle = UIModalPresentationFormSheet;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Verification";
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancel:)] autorelease];
self.messageLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
self.messageLabel.backgroundColor = [UIColor clearColor];
self.messageLabel.font = [UIFont systemFontOfSize:17.0];
self.messageLabel.numberOfLines = 0;
self.messageLabel.text = self.verificationMessage;
self.messageLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.messageLabel];
self.codeField = [[[UITextField alloc] initWithFrame:CGRectZero] autorelease];
self.codeField.borderStyle = UITextBorderStyleRoundedRect;
self.codeField.font = [UIFont systemFontOfSize:24.0];
self.codeField.keyboardType = UIKeyboardTypeNumberPad;
self.codeField.placeholder = @"Verification code";
self.codeField.textAlignment = NSTextAlignmentCenter;
self.codeField.delegate = self;
[self.view addSubview:self.codeField];
self.verifyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.verifyButton setTitle:@"Verify" forState:UIControlStateNormal];
[self.verifyButton addTarget:self action:@selector(verify:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.verifyButton];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.codeField becomeFirstResponder];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat width = MIN(self.view.bounds.size.width - 48.0, 420.0);
CGFloat left = floor((self.view.bounds.size.width - width) / 2.0);
CGFloat top = MAX(28.0, floor((self.view.bounds.size.height - 210.0) / 3.0));
self.messageLabel.frame = CGRectMake(left, top, width, 70.0);
self.codeField.frame = CGRectMake(left, top + 82.0, width, 52.0);
self.verifyButton.frame = CGRectMake(left, top + 148.0, width, 48.0);
}
- (void)verify:(id)sender {
if ([self.codeField.text length] == 0 || self.submitting) {
return;
}
self.submitting = YES;
self.codeField.enabled = NO;
self.verifyButton.enabled = NO;
[self.delegate verificationViewController:self didSubmitCode:self.codeField.text];
}
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSString *updatedCode = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([updatedCode rangeOfCharacterFromSet:nonDigits].location != NSNotFound || [updatedCode length] > 6) {
return NO;
}
if ([updatedCode length] == 6) {
textField.text = updatedCode;
[self verify:textField];
return NO;
}
return YES;
}
- (void)cancel:(id)sender {
[self.delegate verificationViewControllerDidCancel:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self verify:textField];
return YES;
}
- (void)dealloc {
_codeField.delegate = nil;
[_codeField release];
[_messageLabel release];
[_verifyButton release];
[_verificationMessage release];
[super dealloc];
}
@end