2010-10-31 23:02:13 -04:00
|
|
|
//
|
|
|
|
// LoginViewController.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Samuel Clay on 10/31/10.
|
|
|
|
// Copyright 2010 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "LoginViewController.h"
|
2010-11-11 20:05:53 -05:00
|
|
|
#import "NewsBlurAppDelegate.h"
|
2010-11-13 13:42:20 -05:00
|
|
|
#import "ASIHTTPRequest.h"
|
|
|
|
#import "ASIFormDataRequest.h"
|
2010-11-11 20:05:53 -05:00
|
|
|
#import "JSON.h"
|
2010-10-31 23:02:13 -04:00
|
|
|
|
|
|
|
@implementation LoginViewController
|
|
|
|
|
|
|
|
@synthesize appDelegate;
|
|
|
|
@synthesize usernameTextField;
|
|
|
|
@synthesize passwordTextField;
|
2010-11-10 21:54:40 -05:00
|
|
|
@synthesize jsonString;
|
2011-08-13 23:00:51 -07:00
|
|
|
@synthesize activityIndicator;
|
|
|
|
@synthesize authenticatingLabel;
|
|
|
|
@synthesize errorLabel;
|
2010-10-31 23:02:13 -04:00
|
|
|
|
2010-11-11 20:05:53 -05:00
|
|
|
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
|
|
|
|
|
|
|
|
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
|
|
|
|
[appDelegate hideNavigationBar:NO];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-10-31 23:02:13 -04:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
[usernameTextField becomeFirstResponder];
|
2010-11-11 20:05:53 -05:00
|
|
|
|
|
|
|
[appDelegate hideNavigationBar:NO];
|
|
|
|
|
2010-10-31 23:02:13 -04:00
|
|
|
[super viewDidLoad];
|
|
|
|
}
|
|
|
|
|
2011-08-13 23:00:51 -07:00
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
[self.errorLabel setHidden:YES];
|
|
|
|
[self.authenticatingLabel setHidden:YES];
|
|
|
|
[self.activityIndicator stopAnimating];
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
|
|
|
[self.activityIndicator stopAnimating];
|
|
|
|
[super viewDidAppear:animated];
|
|
|
|
}
|
|
|
|
|
2010-10-31 23:02:13 -04:00
|
|
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
|
|
|
|
[textField resignFirstResponder];
|
|
|
|
if(textField == usernameTextField) {
|
|
|
|
[passwordTextField becomeFirstResponder];
|
|
|
|
} else if (textField == passwordTextField) {
|
|
|
|
NSLog(@"Password return");
|
2010-11-11 20:05:53 -05:00
|
|
|
NSLog(@"appdelegate:: %@", [self appDelegate]);
|
2010-11-10 21:54:40 -05:00
|
|
|
[self checkPassword];
|
2010-10-31 23:02:13 -04:00
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2010-11-10 21:54:40 -05:00
|
|
|
- (void)checkPassword {
|
2011-08-13 23:00:51 -07:00
|
|
|
[self.authenticatingLabel setHidden:NO];
|
2011-08-14 00:16:25 -07:00
|
|
|
[self.errorLabel setHidden:YES];
|
2011-08-13 23:00:51 -07:00
|
|
|
[self.activityIndicator startAnimating];
|
2010-11-11 20:05:53 -05:00
|
|
|
NSLog(@"appdelegate:: %@", [self appDelegate]);
|
2011-08-24 10:24:02 -07:00
|
|
|
NSString *urlString = [NSString stringWithFormat:@"http://%@/reader/login",
|
|
|
|
NEWSBLUR_URL];
|
2010-11-13 13:42:20 -05:00
|
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
2010-11-11 23:48:27 -05:00
|
|
|
[[NSHTTPCookieStorage sharedHTTPCookieStorage]
|
|
|
|
setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
|
2010-11-13 13:42:20 -05:00
|
|
|
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
|
|
|
|
[request setPostValue:[usernameTextField text] forKey:@"login-username"];
|
|
|
|
[request setPostValue:[passwordTextField text] forKey:@"login-password"];
|
|
|
|
[request setPostValue:@"login" forKey:@"submit"];
|
|
|
|
[request setPostValue:@"1" forKey:@"api"];
|
|
|
|
[request setDelegate:self];
|
|
|
|
[request startAsynchronous];
|
2010-11-10 21:54:40 -05:00
|
|
|
}
|
|
|
|
|
2010-11-13 13:42:20 -05:00
|
|
|
|
|
|
|
- (void)requestFinished:(ASIHTTPRequest *)request {
|
2011-08-13 23:00:51 -07:00
|
|
|
[self.authenticatingLabel setHidden:YES];
|
|
|
|
[self.activityIndicator stopAnimating];
|
2010-11-13 13:42:20 -05:00
|
|
|
NSString *responseString = [request responseString];
|
2010-11-15 19:40:17 -05:00
|
|
|
NSDictionary *results = [[NSDictionary alloc]
|
|
|
|
initWithDictionary:[responseString JSONValue]];
|
|
|
|
// int statusCode = [request responseStatusCode];
|
|
|
|
int code = [[results valueForKey:@"code"] intValue];
|
|
|
|
if (code == -1) {
|
2011-08-13 23:00:51 -07:00
|
|
|
NSLog(@"Bad login: %@", results);
|
2010-11-13 13:42:20 -05:00
|
|
|
[appDelegate showLogin];
|
2011-08-13 23:00:51 -07:00
|
|
|
[self.errorLabel setText:[results valueForKey:@"message"]];
|
|
|
|
[self.errorLabel setHidden:NO];
|
2010-11-13 13:42:20 -05:00
|
|
|
} else {
|
2010-11-15 19:40:17 -05:00
|
|
|
NSLog(@"Good login");
|
2010-11-13 13:42:20 -05:00
|
|
|
[appDelegate reloadFeedsView];
|
|
|
|
}
|
2010-11-15 19:40:17 -05:00
|
|
|
|
|
|
|
[results release];
|
2010-11-10 21:54:40 -05:00
|
|
|
}
|
|
|
|
|
2010-11-13 13:42:20 -05:00
|
|
|
- (void)requestFailed:(ASIHTTPRequest *)request
|
|
|
|
{
|
|
|
|
NSError *error = [request error];
|
2011-07-24 20:34:54 -07:00
|
|
|
NSLog(@"Error: %@", error);
|
2010-11-10 21:54:40 -05:00
|
|
|
}
|
|
|
|
|
2010-10-31 23:02:13 -04:00
|
|
|
- (void)didReceiveMemoryWarning {
|
|
|
|
// Releases the view if it doesn't have a superview.
|
|
|
|
[super didReceiveMemoryWarning];
|
|
|
|
|
|
|
|
// Release any cached data, images, etc that aren't in use.
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
2011-08-01 09:43:13 -07:00
|
|
|
[appDelegate release];
|
2010-10-31 23:02:13 -04:00
|
|
|
[usernameTextField release];
|
|
|
|
[passwordTextField release];
|
2010-11-10 21:54:40 -05:00
|
|
|
[jsonString release];
|
2010-10-31 23:02:13 -04:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|