NanoTech

Only enable the auth dialog login button once all fields have been filled.

@@ -14,12 +14,12 @@ typedef enum _ASIAuthenticationType { @@ -14,12 +14,12 @@ typedef enum _ASIAuthenticationType {
14 ASIProxyAuthenticationType = 1 14 ASIProxyAuthenticationType = 1
15 } ASIAuthenticationType; 15 } ASIAuthenticationType;
16 16
17 -@interface ASIAuthenticationDialog : UIViewController <UIActionSheetDelegate, UITableViewDelegate, UITableViewDataSource> { 17 +@interface ASIAuthenticationDialog : UIViewController <UIActionSheetDelegate, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> {
18 ASIHTTPRequest *request; 18 ASIHTTPRequest *request;
19 ASIAuthenticationType type; 19 ASIAuthenticationType type;
20 UITableView *tableView; 20 UITableView *tableView;
21 UIViewController *presentingController; 21 UIViewController *presentingController;
22 - CGFloat keyboardHeight; 22 + UIBarButtonItem *loginButton;
23 } 23 }
24 + (void)presentAuthenticationDialogForRequest:(ASIHTTPRequest *)request; 24 + (void)presentAuthenticationDialogForRequest:(ASIHTTPRequest *)request;
25 + (void)presentProxyAuthenticationDialogForRequest:(ASIHTTPRequest *)request; 25 + (void)presentProxyAuthenticationDialogForRequest:(ASIHTTPRequest *)request;
@@ -13,9 +13,17 @@ ASIAuthenticationDialog *sharedDialog = nil; @@ -13,9 +13,17 @@ ASIAuthenticationDialog *sharedDialog = nil;
13 NSLock *dialogLock = nil; 13 NSLock *dialogLock = nil;
14 BOOL isDismissing = NO; 14 BOOL isDismissing = NO;
15 15
  16 +static const NSUInteger kUsernameRow = 0;
  17 +static const NSUInteger kUsernameSection = 0;
  18 +static const NSUInteger kPasswordRow = 1;
  19 +static const NSUInteger kPasswordSection = 0;
  20 +static const NSUInteger kDomainRow = 0;
  21 +static const NSUInteger kDomainSection = 1;
  22 +
16 @interface ASIAuthenticationDialog () 23 @interface ASIAuthenticationDialog ()
17 - (void)show; 24 - (void)show;
18 @property (retain) UITableView *tableView; 25 @property (retain) UITableView *tableView;
  26 +@property (retain) UIBarButtonItem *loginButton;
19 @end 27 @end
20 28
21 @implementation ASIAuthenticationDialog 29 @implementation ASIAuthenticationDialog
@@ -71,6 +79,7 @@ BOOL isDismissing = NO; @@ -71,6 +79,7 @@ BOOL isDismissing = NO;
71 79
72 [request release]; 80 [request release];
73 [tableView release]; 81 [tableView release];
  82 + [loginButton release];
74 [presentingController.view removeFromSuperview]; 83 [presentingController.view removeFromSuperview];
75 [presentingController release]; 84 [presentingController release];
76 [super dealloc]; 85 [super dealloc];
@@ -112,6 +121,21 @@ BOOL isDismissing = NO; @@ -112,6 +121,21 @@ BOOL isDismissing = NO;
112 contentView] subviews] objectAtIndex:0]; 121 contentView] subviews] objectAtIndex:0];
113 } 122 }
114 123
  124 +- (UITextField *)usernameField
  125 +{
  126 + return [self textFieldInRow:kUsernameRow section:kUsernameSection];
  127 +}
  128 +
  129 +- (UITextField *)passwordField
  130 +{
  131 + return [self textFieldInRow:kPasswordRow section:kPasswordSection];
  132 +}
  133 +
  134 +- (UITextField *)domainField
  135 +{
  136 + return [self textFieldInRow:kDomainRow section:kDomainSection];
  137 +}
  138 +
115 #pragma mark show / dismiss 139 #pragma mark show / dismiss
116 140
117 + (void)dismiss 141 + (void)dismiss
@@ -161,8 +185,10 @@ BOOL isDismissing = NO; @@ -161,8 +185,10 @@ BOOL isDismissing = NO;
161 [navItem setTitle:[[[self request] url] host]]; 185 [navItem setTitle:[[[self request] url] host]];
162 } 186 }
163 187
  188 + [self setLoginButton:[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleDone target:self action:@selector(loginWithCredentialsFromDialog:)]];
164 [navItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAuthenticationFromDialog:)] autorelease]]; 189 [navItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAuthenticationFromDialog:)] autorelease]];
165 - [navItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleDone target:self action:@selector(loginWithCredentialsFromDialog:)] autorelease]]; 190 + [navItem setRightBarButtonItem:loginButton];
  191 + loginButton.enabled = NO;
166 192
167 // We show the login form in a table view, similar to Safari's authentication dialog 193 // We show the login form in a table view, similar to Safari's authentication dialog
168 [bar sizeToFit]; 194 [bar sizeToFit];
@@ -192,8 +218,8 @@ BOOL isDismissing = NO; @@ -192,8 +218,8 @@ BOOL isDismissing = NO;
192 218
193 - (void)loginWithCredentialsFromDialog:(id)sender 219 - (void)loginWithCredentialsFromDialog:(id)sender
194 { 220 {
195 - NSString *username = [[self textFieldInRow:0 section:0] text]; 221 + NSString *username = [[self usernameField] text];
196 - NSString *password = [[self textFieldInRow:1 section:0] text]; 222 + NSString *password = [[self passwordField] text];
197 223
198 if ([self type] == ASIProxyAuthenticationType) { 224 if ([self type] == ASIProxyAuthenticationType) {
199 [[self request] setProxyUsername:username]; 225 [[self request] setProxyUsername:username];
@@ -206,7 +232,7 @@ BOOL isDismissing = NO; @@ -206,7 +232,7 @@ BOOL isDismissing = NO;
206 // Handle NTLM domains 232 // Handle NTLM domains
207 NSString *scheme = ([self type] == ASIStandardAuthenticationType) ? [[self request] authenticationScheme] : [[self request] proxyAuthenticationScheme]; 233 NSString *scheme = ([self type] == ASIStandardAuthenticationType) ? [[self request] authenticationScheme] : [[self request] proxyAuthenticationScheme];
208 if ([scheme isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeNTLM]) { 234 if ([scheme isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeNTLM]) {
209 - NSString *domain = [[self textFieldInRow:0 section:2] text]; 235 + NSString *domain = [[self domainField] text];
210 if ([self type] == ASIProxyAuthenticationType) { 236 if ([self type] == ASIProxyAuthenticationType) {
211 [[self request] setProxyDomain:domain]; 237 [[self request] setProxyDomain:domain];
212 } else { 238 } else {
@@ -267,16 +293,17 @@ BOOL isDismissing = NO; @@ -267,16 +293,17 @@ BOOL isDismissing = NO;
267 [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; 293 [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
268 [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; 294 [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
269 [textField setAutocorrectionType:UITextAutocorrectionTypeNo]; 295 [textField setAutocorrectionType:UITextAutocorrectionTypeNo];
  296 + [textField setDelegate:self];
270 297
271 NSUInteger s = [indexPath section]; 298 NSUInteger s = [indexPath section];
272 NSUInteger r = [indexPath row]; 299 NSUInteger r = [indexPath row];
273 300
274 - if (s == 0 && r == 0) { 301 + if (s == kUsernameSection && r == kUsernameRow) {
275 [textField setPlaceholder:@"User"]; 302 [textField setPlaceholder:@"User"];
276 - } else if (s == 0 && r == 1) { 303 + } else if (s == kPasswordSection && r == kPasswordRow) {
277 [textField setPlaceholder:@"Password"]; 304 [textField setPlaceholder:@"Password"];
278 [textField setSecureTextEntry:YES]; 305 [textField setSecureTextEntry:YES];
279 - } else if (s == 1) { 306 + } else if (s == kDomainSection && r == kDomainRow) {
280 [textField setPlaceholder:@"Domain"]; 307 [textField setPlaceholder:@"Domain"];
281 } 308 }
282 [cell.contentView addSubview:textField]; 309 [cell.contentView addSubview:textField];
@@ -307,9 +334,40 @@ BOOL isDismissing = NO; @@ -307,9 +334,40 @@ BOOL isDismissing = NO;
307 return nil; 334 return nil;
308 } 335 }
309 336
  337 +#pragma mark text field delegates
  338 +
  339 +- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  340 +{
  341 + NSString *newString = [[textField text] stringByReplacingCharactersInRange:range withString:string];
  342 + NSArray *fields = [NSArray arrayWithObjects:
  343 + [self usernameField],
  344 + [self passwordField],
  345 + [self domainField], // ends the array early if not set
  346 + nil];
  347 + BOOL allFilled = YES;
  348 +
  349 + for (UITextField *field in fields) {
  350 + NSString *text = nil;
  351 + if (field == textField) {
  352 + text = newString;
  353 + } else {
  354 + text = [field text];
  355 + }
  356 +
  357 + if ([text length] == 0) {
  358 + allFilled = NO;
  359 + break;
  360 + }
  361 + }
  362 +
  363 + loginButton.enabled = allFilled;
  364 + return YES;
  365 +}
  366 +
310 #pragma mark - 367 #pragma mark -
311 368
312 @synthesize request; 369 @synthesize request;
313 @synthesize type; 370 @synthesize type;
314 @synthesize tableView; 371 @synthesize tableView;
  372 +@synthesize loginButton;
315 @end 373 @end