Skip to content

Commit

Permalink
Add support for signing in with an authorization code
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Jul 26, 2012
1 parent c4d99e1 commit d89fef8
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Cheddar for iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
B208C1FA15BF64630080705D /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B208C1F215BF64630080705D /* SystemConfiguration.framework */; };
B208C1FB15BF64630080705D /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B208C1F315BF64630080705D /* UIKit.framework */; };
B208C5FC15BF658D0080705D /* libCheddarKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B208C5BF15BF654B0080705D /* libCheddarKit.a */; };
B23261AA15C207FF00D455EF /* CDIWebSignInViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B23261A915C207FF00D455EF /* CDIWebSignInViewController.m */; };
B28FD2BE15BF730100365CD6 /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B28FD2BD15BF730100365CD6 /* CFNetwork.framework */; };
B28FD2C015BF731100365CD6 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B28FD2BF15BF731000365CD6 /* CoreGraphics.framework */; };
B28FD2C215BF731B00365CD6 /* MessageUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B28FD2C115BF731B00365CD6 /* MessageUI.framework */; };
Expand Down Expand Up @@ -395,6 +396,8 @@
B208C1F215BF64630080705D /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
B208C1F315BF64630080705D /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
B208C21215BF65490080705D /* CheddarKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; path = CheddarKit.xcodeproj; sourceTree = "<group>"; };
B23261A815C207FF00D455EF /* CDIWebSignInViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CDIWebSignInViewController.h; sourceTree = "<group>"; };
B23261A915C207FF00D455EF /* CDIWebSignInViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CDIWebSignInViewController.m; sourceTree = "<group>"; };
B23FA755151FE0710069A0D1 /* Cheddar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Cheddar.app; sourceTree = BUILT_PRODUCTS_DIR; };
B28FD2BD15BF730100365CD6 /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; };
B28FD2BF15BF731000365CD6 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -754,6 +757,8 @@
B208BBB715BF63A80080705D /* CDISignInViewController.m */,
B208BBB815BF63A80080705D /* CDISignUpViewController.h */,
B208BBB915BF63A80080705D /* CDISignUpViewController.m */,
B23261A815C207FF00D455EF /* CDIWebSignInViewController.h */,
B23261A915C207FF00D455EF /* CDIWebSignInViewController.m */,
);
name = Sessions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1157,6 +1162,7 @@
B28FD2E215BF755600365CD6 /* Cheddar.xcdatamodeld in Sources */,
B28FD2E815BF774D00365CD6 /* CDIDefines.m in Sources */,
B2A2B07B15C0BE7300772CF6 /* CDKTask+CheddariOSAdditions.m in Sources */,
B23261AA15C207FF00D455EF /* CDIWebSignInViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
11 changes: 10 additions & 1 deletion Classes/CDIListsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

#import "CDIListsViewController.h"
#import "CDISignUpViewController.h"
#import "CDIListTableViewCell.h"
#import "CDIListViewController.h"
#import "CDICreateListViewController.h"
Expand All @@ -19,6 +18,12 @@
#import "CDIAddListTableViewCell.h"
#import <SSToolkit/UIScrollView+SSToolkitAdditions.h>

#ifdef CHEDDAR_USE_PASSWORD_FLOW
#import "CDISignUpViewController.h"
#else
#import "CDIWebSignInViewController.h"
#endif

@interface CDIListsViewController ()
- (void)_listUpdated:(NSNotification *)notification;
- (void)_currentUserDidChange:(NSNotification *)notification;
Expand Down Expand Up @@ -311,7 +316,11 @@ - (void)_selectListAtIndexPath:(NSIndexPath *)indexPath newList:(BOOL)newList {

- (void)_checkUser {
if (![CDKUser currentUser]) {
#ifdef CHEDDAR_USE_PASSWORD_FLOW
UIViewController *viewController = [[CDISignUpViewController alloc] init];
#else
UIViewController *viewController = [[CDIWebSignInViewController alloc] init];
#endif
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;

Expand Down
15 changes: 15 additions & 0 deletions Classes/CDIWebSignInViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// CDIWebSignInViewController.h
// Cheddar for iOS
//
// Created by Sam Soffes on 7/26/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//

#import "CDISessionsViewController.h"

@interface CDIWebSignInViewController : UIViewController <SSWebViewDelegate>

@property (nonatomic, strong, readonly) SSWebView *webView;

@end
117 changes: 117 additions & 0 deletions Classes/CDIWebSignInViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//
// CDIWebSignInViewController.m
// Cheddar for iOS
//
// Created by Sam Soffes on 7/26/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//

#import "CDIWebSignInViewController.h"
#import "CDILoadingView.h"
#import "UIColor+CheddariOSAdditions.h"
#import <SSToolkit/SSCategories.h>

@interface CDIWebSignInViewController ()
- (void)_authorizeWithCode:(NSString *)code;
@end

@implementation CDIWebSignInViewController {
BOOL _authorizing;
}

@synthesize webView = _webView;

#pragma mark - NSObject

- (id)init {
if ((self = [super init])) {
self.title = @"Cheddar";
UIImageView *title = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nav-title.png"]];
title.frame = CGRectMake(0.0f, 0.0f, 116.0f, 21.0f);
self.navigationItem.titleView = title;
}
return self;
}


#pragma mark - UIViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cheddarArchesColor];

CDILoadingView *loadingView = [[CDILoadingView alloc] initWithFrame:self.view.bounds];
loadingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:loadingView];

_webView = [[SSWebView alloc] initWithFrame:self.view.bounds];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_webView.delegate = self;
_webView.alpha = 0.0f;
[_webView loadURLString:[NSString stringWithFormat:@"https://api.cheddarapp.com/oauth/authorize?client_id=%@", kCDIAPIClientID]];
[self.view addSubview:_webView];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? YES : toInterfaceOrientation == UIInterfaceOrientationPortrait;
}


#pragma mark - Private

- (void)_authorizeWithCode:(NSString *)code {
_authorizing = YES;

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
_webView.alpha = 0.0f;
} completion:nil];

[[CDKHTTPClient sharedClient] signInWithAuthorizationCode:code success:^(AFJSONRequestOperation *operation, id responseObject) {
[self.navigationController dismissModalViewControllerAnimated:YES];
} failure:^(AFJSONRequestOperation *operation, NSError *error) {
NSLog(@"Failed to sign in: %@", error);
[_webView loadURLString:[NSString stringWithFormat:@"https://api.cheddarapp.com/oauth/authorize?client_id=%@", kCDIAPIClientID]];
_authorizing = NO;
}];
}


#pragma mark - SSWebViewDelegate

- (BOOL)webView:(SSWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = aRequest.mainDocumentURL;
if ([url.scheme isEqualToString:@"cheddar"] && [url.host isEqualToString:@"oauth"]) {
NSString *code = [[url queryDictionary] objectForKey:@"code"];
if (code) {
[self _authorizeWithCode:code];
}
return NO;
}

return YES;
}


- (void)webViewDidStartLoadingPage:(SSWebView *)aWebView {
if (_authorizing) {
return;
}

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
_webView.alpha = 1.0f;
} completion:nil];
}


- (void)webViewDidFinishLoadingPage:(SSWebView *)aWebView {
if (_authorizing) {
return;
}

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
_webView.alpha = 1.0f;
} completion:nil];
}

@end
2 changes: 1 addition & 1 deletion Vendor/CheddarKit
2 changes: 1 addition & 1 deletion Vendor/SSToolkit

0 comments on commit d89fef8

Please sign in to comment.