Skip to content

Commit

Permalink
add authorization via verify_credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
mootoh committed Jun 18, 2009
1 parent 12b26e6 commit 5f828e1
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 35 deletions.
5 changes: 4 additions & 1 deletion Classes/AppDelegate.h
Expand Up @@ -8,17 +8,20 @@

#import "DRMapViewController.h"

@class MySelf;
@class MySelf, SettingViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
DRMapViewController *view_controller;
MySelf *my_self;
SettingViewController *setting_view_controller;
}

@property (nonatomic, assign) MySelf *my_self;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UIViewController *view_controller;

- (void) authorized;

@end
49 changes: 43 additions & 6 deletions Classes/AppDelegate.m
Expand Up @@ -9,18 +9,25 @@
#import "AppDelegate.h"
#import "MySelf.h"
#import "DragonRador.h"
#import "SettingViewController.h"

@interface AppDelegate (Private)
- (void) showAuthorization;
@end // AppDelegate

@implementation AppDelegate

@synthesize window, view_controller, my_self;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// create my_self from scratch or from persistent storage.
my_self = [[MySelf alloc] initWithName:[[NSUserDefaults standardUserDefaults] stringForKey:DR_TWITTER_USER] password:[[NSUserDefaults standardUserDefaults] stringForKey:DR_TWITTER_PASSWORD]];

view_controller = [[DRMapViewController alloc] initWithNibName:@"DRMapView" bundle:nil];
[window addSubview:view_controller.view];
if (! [[NSUserDefaults standardUserDefaults] boolForKey:DR_TWITTER_AUTHORIZED]) {
my_self = nil;
// authorize first
[self showAuthorization];
} else {
[self authorized];
}
[window makeKeyAndVisible];
}

Expand All @@ -32,4 +39,34 @@ - (void)dealloc
[super dealloc];
}

@end
- (void) applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] synchronize];
}

- (void) authorized
{
[setting_view_controller release];

// create my_self from scratch or from persistent storage.
my_self = [[MySelf alloc] initWithName:[[NSUserDefaults standardUserDefaults] stringForKey:DR_TWITTER_USER] password:[[NSUserDefaults standardUserDefaults] stringForKey:DR_TWITTER_PASSWORD]];

view_controller = [[DRMapViewController alloc] initWithNibName:@"DRMapView" bundle:nil];
[window addSubview:view_controller.view];
}

@end // AppDelegate

@implementation AppDelegate (Private)

- (void) showAuthorization
{
setting_view_controller = [[SettingViewController alloc] initWithNibName:nil bundle:nil];
//UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:svc];
//[self presentModalViewController:nc animated:YES];
//[nc release];
[window addSubview:setting_view_controller.view];
//[svc release];
}

@end // AppDelegate (Private)
1 change: 1 addition & 0 deletions Classes/DragonRador.h
Expand Up @@ -2,6 +2,7 @@
#define DR_FRIENDS @"friends"
#define DR_TWITTER_USER @"user_name"
#define DR_TWITTER_PASSWORD @"password"
#define DR_TWITTER_AUTHORIZED @"authorized"

//#define LOCATION_SERVER @"http://dragon-rador.appspot.com"
#define LOCATION_SERVER @"http://192.168.1.101:8080"
1 change: 0 additions & 1 deletion Classes/FriendsPickViewController.m
Expand Up @@ -86,7 +86,6 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
}

[[NSUserDefaults standardUserDefaults] setObject:my_self.friends forKey:DR_FRIENDS];
[[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark others
Expand Down
6 changes: 3 additions & 3 deletions Classes/MySelf.h
Expand Up @@ -10,17 +10,17 @@

@class Friend, CLLocation;

@interface MySelf : NSObject <NSCoding>
@interface MySelf : NSObject
{
NSString *twitter_user_name;
NSString *twitter_password;

NSMutableArray *friends; // collection of Friend
NSMutableSet *friends; // collection of Friend
BOOL visible;
}

@property (readonly) BOOL visible;
@property (nonatomic,readonly) NSMutableArray *friends;
@property (nonatomic,readonly) NSMutableSet *friends;

- (id) initWithName:(NSString *)name password:(NSString *)pw;

Expand Down
42 changes: 24 additions & 18 deletions Classes/MySelf.m
Expand Up @@ -12,42 +12,28 @@
#import "DragonRador.h"

@interface MySelf (Private)
- (void) loadFriends;
@end // MySelf (Private)

@implementation MySelf
@synthesize visible, friends;

#define FRIENDS_FILE @"friends.dat"

- (id) initWithName:(NSString *)name password:(NSString *)pw
{
if (self = [super init]) {
twitter_user_name = [name retain];
twitter_password = [pw retain];

NSArray *saved_friends = [[NSUserDefaults standardUserDefaults] objectForKey:DR_FRIENDS];
friends = saved_friends ? [NSMutableArray arrayWithArray:saved_friends] : [NSMutableArray array];
[friends retain];
[self loadFriends];
NSLog(@"friends are %@", friends);

visible = YES; // visible by default
}
return self;
}

- (id) initWithCoder:(NSCoder *)decoder
{
twitter_user_name = [[decoder decodeObjectForKey:@"twitter_user_name"] retain];
twitter_password = [[decoder decodeObjectForKey:@"twitter_password"] retain];
friends = [[decoder decodeObjectForKey:@"friends"] retain];
visible = YES; // visible by default
return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
NSAssert(NO, @"not implemented yet");
// TODO
}

- (void) dealloc
{
[friends release];
Expand Down Expand Up @@ -102,3 +88,23 @@ - (NSArray *) twitterFriends
}

@end

@implementation MySelf (Private)

- (void) loadFriends
{
/*
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:FRIENDS_FILE]) {
NSMutableData *data = [NSMutableData dataWithContentsOfFile:path];
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
self.friends = [decoder decodeObjectForKey:@"friends"];
[decoder finishDecoding];
[decoder release];
} else {
self.friends = [NSMutableSet set];
}
*/
}

@end
47 changes: 41 additions & 6 deletions Classes/SettingViewController.m
Expand Up @@ -8,6 +8,11 @@

#import "SettingViewController.h"
#import "DragonRador.h"
#import "AppDelegate.h"

@interface SettingViewController (Private)
- (BOOL) authorize; // authorize via Twitter verify_credentials
@end // SettingViewController (Private)

@implementation SettingViewController
@synthesize table_view;
Expand All @@ -29,9 +34,9 @@ - (void)viewDidLoad
[super viewDidLoad];
self.title = @"Setting";

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
//UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
//self.navigationItem.leftBarButtonItem = backButton;
//[backButton release];

userField = [[UITextField alloc] initWithFrame:CGRectMake(100, 11, 128, 22)];
NSString *user_name = [[NSUserDefaults standardUserDefaults] stringForKey:DR_TWITTER_USER];
Expand Down Expand Up @@ -75,7 +80,10 @@ - (void) dealloc

- (void) done
{
[self dismissModalViewControllerAnimated:YES];
// [self dismissModalViewControllerAnimated:YES];
AppDelegate *app = [UIApplication sharedApplication].delegate;
[self.view removeFromSuperview];
[app authorized];
}

#pragma mark Table view methods
Expand Down Expand Up @@ -212,12 +220,39 @@ - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInte
return nil;
}

@end // SettingViewController

@implementation SettingViewController (Private)

- (void) commitUserInfo
{
NSLog(@"commitUserInfo");
[[NSUserDefaults standardUserDefaults] setObject:userField.text forKey:DR_TWITTER_USER];
[[NSUserDefaults standardUserDefaults] setObject:passwordField.text forKey:DR_TWITTER_PASSWORD];
[[NSUserDefaults standardUserDefaults] synchronize];

if ([self authorize]) {
[self done];
} else {
// Alert
}
}

- (BOOL) authorize
{
// start activity indicator
// call Twitter verify_credentials API
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@@twitter.com/account/verify_credentials.xml", userField.text, passwordField.text]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSHTTPURLResponse *res = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&err];
if (err) {
// show alert
NSLog(@"error: %@", [err localizedDescription]);
return NO;
}

return res.statusCode == 200;
}

@end
@end // SettingViewController (Private)

0 comments on commit 5f828e1

Please sign in to comment.