-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthenticationViewController.m
211 lines (169 loc) · 6.24 KB
/
AuthenticationViewController.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#import "AuthenticationViewController.h"
#import "LocationsAppDelegate.h"
#import "User.h"
@interface AuthenticationViewController ()
- (UITextField *)newUsernameField;
- (UITextField *)newPasswordField;
- (void)authenticateRemote;
@end
@implementation AuthenticationViewController
@synthesize usernameField;
@synthesize passwordField;
@synthesize user;
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[usernameField release];
[passwordField release];
[user release];
[super dealloc];
}
#pragma mark -
#pragma mark View lifecycle
- (id)initWithCurrentUser:(User *)aUser {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
self.user = aUser;
//self.client = [RKClient clientWithBaseURL:gBaseURL];
//[RKClient setSharedClient:client];
//NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
//[ [RKClient sharedClient] get:@"/" delegate:self];
self.title = @"Login";
self.tableView.allowsSelection = NO;
self.tableView.backgroundColor = TABLE_BACKGROUND_COLOR;
usernameField = [self newUsernameField];
[usernameField becomeFirstResponder];
passwordField = [self newPasswordField];
UIBarButtonItem *saveButton = [AppHelpers newSaveButton:self];
self.navigationItem.rightBarButtonItem = saveButton;
saveButton.enabled = NO;
[saveButton release];
}
#pragma mark -
#pragma mark Actions
- (IBAction)save {
user.login = [usernameField text];
NSLog(@"user login => %@", user.login);
NSLog(@"user field => %@", usernameField.text);
user.password = [passwordField text];
[user saveCredentialsToKeychain];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//[[ConnectionManager sharedInstance] runJob:@selector(authenticateRemote)
// onTarget:self];
//client.username = user.login;
//client.password = user.password;
//client.username = [usernameField text];
//client.password = [passwordField text];
//[[RKClient sharedClient] get:@"/events" delegate:self];
//[[RKClient sharedClient] get:@"/validate" delegate:self];
//NSDictionary* params = [NSDictionary dictionaryWithObject:[usernameField text] forKey:@"login"];
//NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[usernameField text],[passwordField text],nil]
// forKeys:[NSArray arrayWithObjects:@"login",@"password",nil]];
//[[RKClient sharedClient] post:@"/validate" params:params delegate:self];
}
/*
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
if ([request isGET]) {
// Handling GET /foo.xml
if ([response isOK]) {
// Success! Let's take a look at the data
NSLog(@"Retrieved XML: %@", [response bodyAsString]);
}
} else if ([request isPOST]) {
// Handling POST /other.json
if ([response isJSON]) {
NSLog(@"Got a JSON response back from our POST!");
}
} else if ([request isDELETE]) {
// Handling DELETE /missing_resource.txt
if ([response isNotFound]) {
NSLog(@"The resource path '%@' was not found.", [request resourcePath]);
}
}
}
*/
#pragma mark -
#pragma mark Text Field Delegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
if (textField == usernameField) {
[passwordField becomeFirstResponder];
}
if (textField == passwordField) {
[self save];
}
return YES;
}
- (IBAction)textFieldChanged:(id)sender {
BOOL enableSaveButton =
([self.usernameField.text length] > 0) && ([self.passwordField.text length] > 0);
[self.navigationItem.rightBarButtonItem setEnabled:enableSaveButton];
}
#pragma mark -
#pragma mark Table data source methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (NSString *)tableView:(UITableView *)tableView
titleForFooterInSection:(NSInteger)section {
return @"\nEnter the username and password for your online account.";
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =
[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil] autorelease];
if (indexPath.row == 0) {
[cell.contentView addSubview:usernameField];
} else {
[cell.contentView addSubview:passwordField];
}
return cell;
}
#pragma mark -
#pragma mark Private methods
- (void)authenticateRemote {
NSError *error = nil;
BOOL authenticated = [user authenticate:&error];
if (authenticated == YES) {
[user saveCredentialsToKeychain];
[self.navigationController performSelectorOnMainThread:@selector(popViewControllerAnimated:)
withObject:[NSNumber numberWithBool:YES]
waitUntilDone:NO];
[AppHelpers showAlert:@"Welcome!" withMessage:@"Your account information was saved."];
} else {
[AppHelpers handleRemoteError:error];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (UITextField *)newUsernameField {
UITextField *field = [AppHelpers newTableCellTextField:self];
field.placeholder = @"Username";
field.text = self.user.login;
field.autocapitalizationType = UITextAutocapitalizationTypeNone;
field.autocorrectionType = UITextAutocorrectionTypeNo;
field.returnKeyType = UIReturnKeyNext;
[field addTarget:self
action:@selector(textFieldChanged:)
forControlEvents:UIControlEventEditingChanged];
return field;
}
- (UITextField *)newPasswordField {
UITextField *field = [AppHelpers newTableCellTextField:self];
field.placeholder = @"Password";
field.text = self.user.password;
field.autocapitalizationType = UITextAutocapitalizationTypeNone;
field.autocorrectionType = UITextAutocorrectionTypeNo;
field.secureTextEntry = YES;
field.returnKeyType = UIReturnKeyDone;
[field addTarget:self
action:@selector(textFieldChanged:)
forControlEvents:UIControlEventEditingChanged];
return field;
}
@end