Skip to content

Commit

Permalink
Starting point for new iPhone and iPad sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
pokeb committed Jun 17, 2010
1 parent e5a0ed8 commit db937d3
Show file tree
Hide file tree
Showing 35 changed files with 2,349 additions and 2,900 deletions.
2 changes: 1 addition & 1 deletion Classes/ASIHTTPRequest.m
Expand Up @@ -23,7 +23,7 @@


// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.6.2-16 2010-06-15";
NSString *ASIHTTPRequestVersion = @"v1.6.2-17 2010-06-17";

NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";

Expand Down
19 changes: 10 additions & 9 deletions iPhone Sample/AuthenticationViewController.h
@@ -1,23 +1,24 @@
//
// AuthenticationViewController.h
// iPhone
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details
//
// Created by Ben Copsey on 01/08/2009.
// Copyright 2009 All-Seeing Interactive. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SampleViewController.h"
@class ASIHTTPRequest;

@interface AuthenticationViewController : UIViewController {
IBOutlet UISwitch *useKeychain;
IBOutlet UISwitch *useBuiltInDialog;
IBOutlet UILabel *topSecretInfo;
ASIHTTPRequest *requestRequiringAuthentication;
ASIHTTPRequest *requestRequiringProxyAuthentication;
@interface AuthenticationViewController : SampleViewController {

ASIHTTPRequest *request;

UISwitch *useKeychain;
UISwitch *useBuiltInDialog;
UITextView *responseField;
}
- (IBAction)fetchTopSecretInformation:(id)sender;

@property (retain) ASIHTTPRequest *requestRequiringAuthentication;
@property (retain) ASIHTTPRequest *requestRequiringProxyAuthentication;
@property (retain, nonatomic) ASIHTTPRequest *request;
@end
176 changes: 146 additions & 30 deletions iPhone Sample/AuthenticationViewController.m
@@ -1,19 +1,22 @@
//
// AuthenticationViewController.m
// iPhone
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details
//
// Created by Ben Copsey on 01/08/2009.
// Copyright 2009 All-Seeing Interactive. All rights reserved.
//

#import "AuthenticationViewController.h"
#import "ASIHTTPRequest.h"
#import "InfoCell.h"
#import "DetailCell.h"
#import "ToggleCell.h"

@implementation AuthenticationViewController

- (IBAction)fetchTopSecretInformation:(id)sender
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/top_secret/"]];
[self setRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/top_secret/"]]];
[request setUseKeychainPersistence:[useKeychain isOn]];
[request setDelegate:self];
[request setShouldPresentAuthenticationDialog:[useBuiltInDialog isOn]];
Expand All @@ -23,25 +26,20 @@ - (IBAction)fetchTopSecretInformation:(id)sender

}

- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)request
- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest
{
[topSecretInfo setText:[[request error] localizedDescription]];
[topSecretInfo setFont:[UIFont boldSystemFontOfSize:12]];
[responseField setText:[[request error] localizedDescription]];
[responseField setFont:[UIFont boldSystemFontOfSize:12]];
}

- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)request
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest
{
[topSecretInfo setText:[request responseString]];
[topSecretInfo setFont:[UIFont boldSystemFontOfSize:12]];
[responseField setText:[request responseString]];
[responseField setFont:[UIFont boldSystemFontOfSize:12]];
}

- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request
- (void)authenticationNeededForRequest:(ASIHTTPRequest *)theRequest
{
// Why oh why is there no contextInfo for alertView like on Mac OS ?!
[self setRequestRequiringProxyAuthentication:nil];
[self setRequestRequiringAuthentication:request];


UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Please Login" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil] autorelease];
// These are undocumented, use at your own risk!
// A better general approach would be to subclass UIAlertView, or just use ASIHTTPRequest's built-in dialog
Expand All @@ -51,10 +49,8 @@ - (void)authenticationNeededForRequest:(ASIHTTPRequest *)request

}

- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request
- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)theRequest
{
[self setRequestRequiringAuthentication:nil];
[self setRequestRequiringProxyAuthentication:request];
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Please Login to proxy" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil] autorelease];
[alertView addTextFieldWithValue:@"" label:@"Username"];
[alertView addTextFieldWithValue:@"" label:@"Password"];
Expand All @@ -66,17 +62,17 @@ - (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
if ([self requestRequiringAuthentication]) {
[[self requestRequiringAuthentication] setUsername:[[alertView textFieldAtIndex:0] text]];
[[self requestRequiringAuthentication] setPassword:[[alertView textFieldAtIndex:1] text]];
[[self requestRequiringAuthentication] retryUsingSuppliedCredentials];
} else if ([self requestRequiringProxyAuthentication]) {
[[self requestRequiringProxyAuthentication] setProxyUsername:[[alertView textFieldAtIndex:0] text]];
[[self requestRequiringProxyAuthentication] setProxyPassword:[[alertView textFieldAtIndex:1] text]];
[[self requestRequiringProxyAuthentication] retryUsingSuppliedCredentials];
if ([[self request] authenticationNeeded] == ASIHTTPAuthenticationNeeded) {
[[self request] setUsername:[[alertView textFieldAtIndex:0] text]];
[[self request] setPassword:[[alertView textFieldAtIndex:1] text]];
[[self request] retryUsingSuppliedCredentials];
} else if ([[self request] authenticationNeeded] == ASIProxyAuthenticationNeeded) {
[[self request] setProxyUsername:[[alertView textFieldAtIndex:0] text]];
[[self request] setProxyPassword:[[alertView textFieldAtIndex:1] text]];
[[self request] retryUsingSuppliedCredentials];
}
} else {
[[self requestRequiringAuthentication] cancelAuthentication];
[[self request] cancelAuthentication];
}
}

Expand All @@ -99,11 +95,131 @@ - (void)didReceiveMemoryWarning {

- (void)dealloc
{
[requestRequiringAuthentication release];
[requestRequiringProxyAuthentication release];
[request cancel];
[request release];
[super dealloc];
}

@synthesize requestRequiringAuthentication;
@synthesize requestRequiringProxyAuthentication;
/*
Most of the code below here relates to the table view, and isn't that interesting
*/

- (void)viewDidLoad
{
[[[self navigationBar] topItem] setTitle:@"HTTP Authentication"];
}

static NSString *intro = @"Demonstrates fetching content from an area that requires HTTP authentication. You will be prompted for a username and password, enter 'topsecret' for both.\nIf you turn on keychain support, successful authentication will result in the username and password you provided being stored in your keychain. The application will use these details rather than prompt you the next time.\nToggle 'Use built-in dialog' to switch between ASIHTTPRequest's built-in dialog, and one created by the delegate.";

- (UIView *)tableView:(UITableView *)theTableView viewForHeaderInSection:(NSInteger)section
{
if (section == 1) {
int tablePadding = 40;
int tableWidth = [tableView frame].size.width;
if (tableWidth > 480) { // iPad
tablePadding = 110;
}

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableWidth-(tablePadding/2),30)] autorelease];
UIButton *goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[goButton setTitle:@"Go!" forState:UIControlStateNormal];
[goButton sizeToFit];
[goButton setFrame:CGRectMake([view frame].size.width-[goButton frame].size.width+10,7,[goButton frame].size.width,[goButton frame].size.height)];

[goButton addTarget:self action:@selector(fetchTopSecretInformation:) forControlEvents:UIControlEventTouchDown];
[view addSubview:goButton];

return view;
}
return nil;
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if ([indexPath section] == 0) {
cell = [InfoCell cellWithDescription:intro];
} else if ([indexPath section] == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:@"ToggleCell"];
if (!cell) {
cell = [ToggleCell cell];
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"] autorelease];
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

int tablePadding = 40;
int tableWidth = [tableView frame].size.width;
if (tableWidth > 480) { // iPad
tablePadding = 110;
}

switch ([indexPath section]) {
case 1:
switch ([indexPath row]) {
case 0:
[[cell textLabel] setText:@"Use Keychain"];
useKeychain = [(ToggleCell *)cell toggle];
break;
case 1:
[[cell textLabel] setText:@"Use Built-In Dialog"];
useBuiltInDialog = [(ToggleCell *)cell toggle];
break;
}
break;
case 2:
responseField = [[[UITextView alloc] initWithFrame:CGRectMake(5,5,tableWidth-tablePadding,150)] autorelease];
[responseField setBackgroundColor:[UIColor clearColor]];
[responseField setText:@"Secret information will appear here if authentication succeeds"];
[[cell contentView] addSubview:responseField];
break;
}
return cell;
}

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
if (section == 1) {
return 2;
} else {
return 1;
}
}

- (CGFloat)tableView:(UITableView *)theTableView heightForHeaderInSection:(NSInteger)section
{
if (section == 1) {
return 50;
}
return 34;
}

- (CGFloat)tableView:(UITableView *)theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath section] == 0) {
return [InfoCell neededHeightForDescription:intro withTableWidth:[tableView frame].size.width]+20;
} else if ([indexPath section] == 2) {
return 160;
} else {
return 42;
}
}

- (NSString *)tableView:(UITableView *)theTableView titleForHeaderInSection:(NSInteger)section
{

return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}


@synthesize request;
@end
17 changes: 17 additions & 0 deletions iPhone Sample/DetailCell.h
@@ -0,0 +1,17 @@
//
// DetailCell.h
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details
//
// Created by Ben Copsey on 16/06/2010.
// Copyright 2010 All-Seeing Interactive. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface DetailCell : UITableViewCell {

}
+ (id)cell;

@end
35 changes: 35 additions & 0 deletions iPhone Sample/DetailCell.m
@@ -0,0 +1,35 @@
//
// DetailCell.m
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details
//
// Created by Ben Copsey on 16/06/2010.
// Copyright 2010 All-Seeing Interactive. All rights reserved.
//

#import "DetailCell.h"


@implementation DetailCell

+ (id)cell
{
DetailCell *cell = [[[DetailCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"HeaderCell"] autorelease];
[[cell detailTextLabel] setTextAlignment:UITextAlignmentLeft];
[[cell detailTextLabel] setFont:[UIFont systemFontOfSize:14]];
return cell;
}

- (void)layoutSubviews
{
[super layoutSubviews];
int tablePadding = 40;
int tableWidth = [[self superview] frame].size.width;
if (tableWidth > 480) { // iPad
tablePadding = 110;
}
[[self textLabel] setFrame:CGRectMake(5,5,120,20)];
[[self detailTextLabel] setFrame:CGRectMake(135,5,[self frame].size.width-145-tablePadding,20)];

}

@end
17 changes: 17 additions & 0 deletions iPhone Sample/InfoCell.h
@@ -0,0 +1,17 @@
//
// InfoCell.h
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details
//
// Created by Ben Copsey on 17/06/2010.
// Copyright 2010 All-Seeing Interactive. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface InfoCell : UITableViewCell {

}
+ (id)cellWithDescription:(NSString *)description;
+ (NSUInteger)neededHeightForDescription:(NSString *)description withTableWidth:(NSUInteger)tableWidth;
@end

0 comments on commit db937d3

Please sign in to comment.