-
Notifications
You must be signed in to change notification settings - Fork 0
/
AFAuthenticationAlertView.m
89 lines (77 loc) · 4 KB
/
AFAuthenticationAlertView.m
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
//
// AFAuthenticationAlertView.m
// WordPress
//
// Created by Jorge Bernal on 3/15/12.
// Copyright (c) 2012 WordPress. All rights reserved.
//
#import "AFAuthenticationAlertView.h"
@implementation AFAuthenticationAlertView {
NSURLAuthenticationChallenge *_challenge;
UITextField *usernameField, *passwordField;
}
- (void)dealloc {
[_challenge release];
[usernameField release];
[passwordField release];
[super dealloc];
}
- (id)initWithChallenge:(NSURLAuthenticationChallenge *)challenge {
self = [super init];
if (self) {
_challenge = [challenge retain];
[self initWithTitle:NSLocalizedString(@"Authentication required", @"Popup title to ask for user credentials.")
message:NSLocalizedString(@"Please enter your credentials", @"Popup message to ask for user credentials (fields shown below).")
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button label.")
otherButtonTitles:NSLocalizedString(@"Log In", @"Log In button label."), nil];
// FIXME: what about iOS 4?
if ([self respondsToSelector:@selector(setAlertViewStyle:)]) {
self.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
} else {
if (IS_IPAD) {
self.message = [self.message stringByAppendingString:@"\n\n\n\n"];
} else {
self.message = [self.message stringByAppendingString:@"\n\n\n"];
}
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(12.0f, 48.0f, 260.0f, 29.0f)];
usernameField.placeholder = NSLocalizedString(@"Username", @"Field label for a user's username.");
usernameField.backgroundColor = [UIColor whiteColor];
usernameField.textColor = [UIColor blackColor];
usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
usernameField.keyboardType = UIKeyboardTypeDefault;
usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[self addSubview:usernameField];
passwordField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 82.0, 260.0, 29.0)];
passwordField.placeholder = NSLocalizedString(@"Password", @"Field label for a user's password.");
passwordField.secureTextEntry = YES;
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.textColor = [UIColor blackColor];
passwordField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
passwordField.keyboardType = UIKeyboardTypeDefault;
passwordField.autocorrectionType = UITextAutocorrectionTypeNo;
[self addSubview:passwordField];
}
}
return self;
}
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
if (buttonIndex == 1) {
NSString *username, *password;
if ([self respondsToSelector:@selector(setAlertViewStyle:)]) {
username = [[self textFieldAtIndex:0] text];
password = [[self textFieldAtIndex:1] text];
} else {
username = usernameField.text;
password = passwordField.text;
}
NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistencePermanent];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:[_challenge protectionSpace]];
[[_challenge sender] useCredential:credential forAuthenticationChallenge:_challenge];
} else {
[[_challenge sender] cancelAuthenticationChallenge:_challenge];
}
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
@end