AuthenticationViewController.m
4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// AuthenticationViewController.m
// iPhone
//
// Created by Ben Copsey on 01/08/2009.
// Copyright 2009 All-Seeing Interactive. All rights reserved.
//
#import "AuthenticationViewController.h"
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
@implementation AuthenticationViewController
- (void)awakeFromNib
{
[self setNetworkQueue:[[[ASINetworkQueue alloc] init] autorelease]];
}
- (IBAction)fetchTopSecretInformation:(id)sender
{
[networkQueue cancelAllOperations];
[networkQueue setRequestDidFinishSelector:@selector(topSecretFetchComplete:)];
[networkQueue setRequestDidFailSelector:@selector(topSecretFetchFailed:)];
[networkQueue setDelegate:self];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/top_secret/"]];
[request setUseKeychainPersistance:[useKeychain isOn]];
[request setShouldPresentAuthenticationDialog:[useBuiltInDialog isOn]];
[networkQueue addOperation:request];
[networkQueue go];
}
- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)request
{
[topSecretInfo setText:[[request error] localizedDescription]];
[topSecretInfo setFont:[UIFont boldSystemFontOfSize:12]];
}
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)request
{
[topSecretInfo setText:[request responseString]];
[topSecretInfo setFont:[UIFont boldSystemFontOfSize:12]];
}
- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request
{
// 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
[alertView addTextFieldWithValue:@"" label:@"Username"];
[alertView addTextFieldWithValue:@"" label:@"Password"];
[alertView show];
}
- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request
{
[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"];
[alertView show];
}
- (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];
}
} else {
[[self requestRequiringAuthentication] cancelAuthentication];
}
}
- (BOOL)respondsToSelector:(SEL)selector
{
if (selector == @selector(authenticationNeededForRequest:) || selector == @selector(proxyAuthenticationNeededForRequest:)) {
if ([useBuiltInDialog isOn]) {
return NO;
}
return YES;
}
return [super respondsToSelector:selector];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc
{
[networkQueue release];
[requestRequiringAuthentication release];
[requestRequiringProxyAuthentication release];
[super dealloc];
}
@synthesize networkQueue;
@synthesize requestRequiringAuthentication;
@synthesize requestRequiringProxyAuthentication;
@end