diff --git a/Authentication.h.sample b/Authentication.h.sample index 7a44dfe..6cdda88 100644 --- a/Authentication.h.sample +++ b/Authentication.h.sample @@ -1,3 +1 @@ -#define API_USER @"bob" -#define API_PASS @"secret" #define API_KEY @"secret" diff --git a/Classes/Controllers/AuthenticationViewController.h b/Classes/Controllers/AuthenticationViewController.h index 5343bc6..97ead9d 100644 --- a/Classes/Controllers/AuthenticationViewController.h +++ b/Classes/Controllers/AuthenticationViewController.h @@ -15,6 +15,10 @@ @property (nonatomic, retain) IBOutlet UITableViewCell *passwordCell; @property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton; ++ (BOOL)signIn; ++ (void)retrieveUsername:(NSString **)username password:(NSString **)password; ++ (void)authenticate:(NSString *)username password:(NSString *)password; + - (IBAction)dismissDialog:(id)sender; - (IBAction)saveCredentials:(id)sender; - (IBAction)textDidChange:(id)sender; diff --git a/Classes/Controllers/AuthenticationViewController.m b/Classes/Controllers/AuthenticationViewController.m index 25f9b55..a9a7b64 100644 --- a/Classes/Controllers/AuthenticationViewController.m +++ b/Classes/Controllers/AuthenticationViewController.m @@ -1,18 +1,47 @@ #import "AuthenticationViewController.h" #import "Trakt.h" -//#import "SSKeychain.h" +#import "SSKeychain.h" @implementation AuthenticationViewController + ++ (BOOL)signIn { + NSString *username, *password; + [self retrieveUsername:&username password:&password]; + if (username && password) { + NSLog(@"User: %@ Password: %@", username, password); + [self authenticate:username password:password]; + return YES; + } + return NO; +} + ++ (void)retrieveUsername:(NSString **)username password:(NSString **)password { + *password = nil; + *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"Username"]; + if (*username) { + *password = [SSKeychain passwordForService:@"iTrakt" account:*username]; + } +} + ++ (void)authenticate:(NSString *)username password:(NSString *)password { + [[Trakt sharedInstance] setApiUser:username]; + [[Trakt sharedInstance] setApiPassword:password]; +} + + @synthesize usernameField, passwordField; @synthesize usernameCell, passwordCell; @synthesize doneButton; -- (void)viewDidAppear:(BOOL)animated { - [super viewDidAppear:animated]; - NSLog(@"Hier!"); - self.usernameField.text = [[Trakt sharedInstance] apiUser]; +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; + + NSString *username, *password; + [AuthenticationViewController retrieveUsername:&username password:&password]; + self.usernameField.text = username; + self.passwordField.text = password; } @@ -58,12 +87,15 @@ - (IBAction)dismissDialog:(id)sender { - (IBAction)saveCredentials:(id)sender { NSLog(@"Save!"); - [[Trakt sharedInstance] setApiUser:self.usernameField.text]; - [[Trakt sharedInstance] setApiPassword:self.passwordField.text]; + NSString *username = self.usernameField.text; + NSString *password = self.passwordField.text; + + [AuthenticationViewController authenticate:username password:password]; - // TODO check if the credentials are correct somehow! - // Where do we store the account? prefs?? - //[SSKeychain setPassword:self.passwordField.text forService:@"iTrakt" account:self.usernameField.text]; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + [defaults setValue:username forKey:@"Username"]; // TODO move to constant + [defaults synchronize]; + [SSKeychain setPassword:password forService:@"iTrakt" account:username]; [self dismissDialog:sender]; } diff --git a/Classes/Controllers/iTraktAppDelegate.m b/Classes/Controllers/iTraktAppDelegate.m index 2380492..1be2570 100644 --- a/Classes/Controllers/iTraktAppDelegate.m +++ b/Classes/Controllers/iTraktAppDelegate.m @@ -20,10 +20,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( //NSLog(@"[!] Clearing cache"); //[[EGOCache currentCache] clearCache]; - // TODO replace this with actual credentials - //[SSKeychain passwordForService:@"iTrakt" account:@""]; TODO hmm, account? - [[Trakt sharedInstance] setApiUser:API_USER]; - [[Trakt sharedInstance] setApiPassword:API_PASS]; + if ([AuthenticationViewController signIn]) { + NSLog(@"Signed in."); + } else { + NSLog(@"Not signed in."); + } + [[Trakt sharedInstance] setApiKey:API_KEY]; [HTTPDownload setGlobalDelegate:self];