Statistics
| Branch: | Revision:

root / asi-http-request-with-pithos / iPhone Sample / AuthenticationViewController.m @ be116d22

History | View | Annotate | Download (7.7 kB)

1
//
2
//  AuthenticationViewController.m
3
//  Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details
4
//
5
//  Created by Ben Copsey on 01/08/2009.
6
//  Copyright 2009 All-Seeing Interactive. All rights reserved.
7
//
8

    
9
#import "AuthenticationViewController.h"
10
#import "ASIHTTPRequest.h"
11
#import "InfoCell.h"
12
#import "DetailCell.h"
13
#import "ToggleCell.h"
14

    
15
@interface UIAlertView (SPI)
16
- (void) addTextFieldWithValue:(NSString *) value label:(NSString *) label;
17
- (void) addTextFieldAtIndex:(NSUInteger) index;
18
- (UITextField *) textFieldAtIndex:(NSUInteger) index;
19
@end
20

    
21
// Private stuff
22
@interface AuthenticationViewController ()
23
- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest;
24
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest;
25
@end
26

    
27
@implementation AuthenticationViewController
28

    
29
- (IBAction)fetchTopSecretInformation:(id)sender
30
{
31
	[self setRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/top_secret/"]]];
32
	[request setUseKeychainPersistence:[useKeychain isOn]];
33
	[request setDelegate:self];
34
	[request setShouldPresentAuthenticationDialog:[useBuiltInDialog isOn]];
35
	[request setDidFinishSelector:@selector(topSecretFetchComplete:)];
36
	[request setDidFailSelector:@selector(topSecretFetchFailed:)];
37
	[request startAsynchronous];
38
}
39

    
40
- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest
41
{
42
	[responseField setText:[[request error] localizedDescription]];
43
	[responseField setFont:[UIFont boldSystemFontOfSize:12]];
44
}
45

    
46
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest
47
{
48
	[responseField setText:[request responseString]];
49
	[responseField setFont:[UIFont boldSystemFontOfSize:12]];
50
}
51

    
52
- (void)authenticationNeededForRequest:(ASIHTTPRequest *)theRequest
53
{
54
	UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Please Login" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil] autorelease];
55
	// These are undocumented, use at your own risk!
56
	// A better general approach would be to subclass UIAlertView, or just use ASIHTTPRequest's built-in dialog
57
	[alertView addTextFieldWithValue:@"" label:@"Username"];
58
	[alertView addTextFieldWithValue:@"" label:@"Password"];
59
	[alertView show];
60

    
61
}
62

    
63
- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)theRequest
64
{
65
	UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Please Login to proxy" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil] autorelease];
66
	[alertView addTextFieldWithValue:@"" label:@"Username"];
67
	[alertView addTextFieldWithValue:@"" label:@"Password"];
68
	[alertView show];
69
}
70

    
71

    
72

    
73
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
74
{
75
	if (buttonIndex == 1) {
76
		if ([[self request] authenticationNeeded] == ASIHTTPAuthenticationNeeded) {
77
			[[self request] setUsername:[[alertView textFieldAtIndex:0] text]];
78
			[[self request] setPassword:[[alertView textFieldAtIndex:1] text]];
79
			[[self request] retryUsingSuppliedCredentials];
80
		} else if ([[self request] authenticationNeeded] == ASIProxyAuthenticationNeeded) {
81
			[[self request] setProxyUsername:[[alertView textFieldAtIndex:0] text]];
82
			[[self request] setProxyPassword:[[alertView textFieldAtIndex:1] text]];
83
			[[self request] retryUsingSuppliedCredentials];
84
		}
85
	} else {
86
		[[self request] cancelAuthentication];
87
	}
88
}
89

    
90
- (BOOL)respondsToSelector:(SEL)selector
91
{
92
	if (selector == @selector(authenticationNeededForRequest:) || selector == @selector(proxyAuthenticationNeededForRequest:)) {
93
		if ([useBuiltInDialog isOn]) {
94
			return NO;
95
		}
96
		return YES;
97
	}
98
	return [super respondsToSelector:selector];
99
}
100

    
101

    
102
- (void)didReceiveMemoryWarning {
103
    [super didReceiveMemoryWarning];
104
}
105

    
106

    
107
- (void)dealloc
108
{
109
	[request cancel];
110
	[request release];
111
	[responseField release];
112
    [super dealloc];
113
}
114

    
115
/*
116
 Most of the code below here relates to the table view, and isn't that interesting
117
 */
118

    
119
- (void)viewDidLoad
120
{
121
	[super viewDidLoad];
122
	[[[self navigationBar] topItem] setTitle:@"HTTP Authentication"];
123
	responseField = [[UITextView alloc] initWithFrame:CGRectZero];
124
	[responseField setBackgroundColor:[UIColor clearColor]];
125
	[responseField setEditable:NO];
126
	[responseField setText:@"Secret information will appear here if authentication succeeds"];
127
}
128

    
129
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.";
130

    
131
- (UIView *)tableView:(UITableView *)theTableView viewForHeaderInSection:(NSInteger)section
132
{
133
	if (section == 1) {
134
		int tablePadding = 40;
135
		int tableWidth = [tableView frame].size.width;
136
		if (tableWidth > 480) { // iPad
137
			tablePadding = 110;
138
		}
139
		
140
		UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableWidth-(tablePadding/2),30)] autorelease];
141
		UIButton *goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
142
		[goButton setTitle:@"Go!" forState:UIControlStateNormal];
143
		[goButton sizeToFit];
144
		[goButton setFrame:CGRectMake([view frame].size.width-[goButton frame].size.width+10,7,[goButton frame].size.width,[goButton frame].size.height)];
145

    
146
		[goButton addTarget:self action:@selector(fetchTopSecretInformation:) forControlEvents:UIControlEventTouchDown];
147
		[view addSubview:goButton];
148
		
149
		return view;
150
	}
151
	return nil;
152
}
153

    
154
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
155
{
156
	int tablePadding = 40;
157
	int tableWidth = [tableView frame].size.width;
158
	if (tableWidth > 480) { // iPad
159
		tablePadding = 110;
160
	}
161
	
162
	
163
	UITableViewCell *cell;
164
	if ([indexPath section] == 0) {
165
		
166
		cell = [tableView dequeueReusableCellWithIdentifier:@"InfoCell"];
167
		if (!cell) {
168
			cell = [InfoCell cell];	
169
		}
170
		[[cell textLabel] setText:intro];
171
		[cell layoutSubviews];
172
		
173
	} else if ([indexPath section] == 1) {
174
		cell = [tableView dequeueReusableCellWithIdentifier:@"ToggleCell"];
175
		if (!cell) {
176
			cell = [ToggleCell cell];
177
			if ([indexPath row] == 0) {
178
				[[cell textLabel] setText:@"Use Keychain"];
179
				useKeychain = [(ToggleCell *)cell toggle];
180
			} else {
181
				[[cell textLabel] setText:@"Use Built-In Dialog"];
182
				useBuiltInDialog = [(ToggleCell *)cell toggle];
183
			}
184
		}
185

    
186
	} else {
187
		
188
		cell = [tableView dequeueReusableCellWithIdentifier:@"Response"];
189
		if (!cell) {
190
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Response"] autorelease];
191
			
192

    
193
			[[cell contentView] addSubview:responseField];
194
		}	
195
		[responseField setFrame:CGRectMake(5,5,tableWidth-tablePadding,150)];
196
		
197

    
198
	}
199
	[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
200
	
201

    
202
	return cell;
203
}
204

    
205
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
206
{
207
	if (section == 1) {
208
		return 2;
209
	} else {
210
		return 1;
211
	}
212
}
213

    
214
- (CGFloat)tableView:(UITableView *)theTableView heightForHeaderInSection:(NSInteger)section
215
{
216
	if (section == 1) {
217
		return 50;
218
	}
219
	return 34;
220
}
221

    
222
- (CGFloat)tableView:(UITableView *)theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
223
{
224
	if ([indexPath section] == 0) {
225
		return [InfoCell neededHeightForDescription:intro withTableWidth:[tableView frame].size.width]+20;
226
	} else if ([indexPath section] == 2) {
227
		return 160;
228
	} else {
229
		return 42;
230
	}
231
}
232

    
233
- (NSString *)tableView:(UITableView *)theTableView titleForHeaderInSection:(NSInteger)section
234
{
235
	return nil;
236
}
237

    
238
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
239
{
240
	return 3;
241
}
242

    
243

    
244
@synthesize request;
245
@end