forked from pokeb/asi-http-request
/
SynchronousViewController.m
176 lines (147 loc) · 5.08 KB
/
SynchronousViewController.m
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// SynchronousViewController.m
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details
//
// Created by Ben Copsey on 07/11/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import "SynchronousViewController.h"
#import "ASIHTTPRequest.h"
#import "DetailCell.h"
#import "InfoCell.h"
@implementation SynchronousViewController
// Runs a request synchronously
- (IBAction)simpleURLFetch:(id)sender
{
NSURL *url = [NSURL URLWithString:[urlField text]];
// Create a request
// You don't normally need to retain a synchronous request, but we need to in this case because we'll need it later if we reload the table data
[self setRequest:[ASIHTTPRequest requestWithURL:url]];
//Customise our user agent, for no real reason
[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];
// Start the request
[request startSynchronous];
// Request has now finished
[[self tableView] reloadData];
}
/*
Most of the code below here relates to the table view, and isn't that interesting
*/
- (void)viewDidLoad
{
[super viewDidLoad];
[[[self navigationBar] topItem] setTitle:@"Synchronous Requests"];
}
- (void)dealloc
{
[request cancel];
[request release];
[super dealloc];
}
static NSString *intro = @"Demonstrates fetching a web page synchronously, the HTML source will appear in the box below when the download is complete. The interface will lock up when you press this button until the operation times out or succeeds. You should avoid using synchronous requests on the main thread, even for the simplest operations.";
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int tablePadding = 40;
int tableWidth = [tableView frame].size.width;
if (tableWidth > 480) { // iPad
tablePadding = 110;
}
UITableViewCell *cell;
if ([indexPath section] == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"InfoCell"];
if (!cell) {
cell = [InfoCell cell];
}
[[cell textLabel] setText:intro];
[cell layoutSubviews];
} else if ([indexPath section] == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:@"URLCell"];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"URLCell"] autorelease];
urlField = [[[UITextField alloc] initWithFrame:CGRectZero] autorelease];
[[cell contentView] addSubview:urlField];
goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[goButton setTitle:@"Go!" forState:UIControlStateNormal];
[goButton addTarget:self action:@selector(simpleURLFetch:) forControlEvents:UIControlEventTouchUpInside];
[[cell contentView] addSubview:goButton];
}
[goButton setFrame:CGRectMake(tableWidth-tablePadding-38,7,20,20)];
[goButton sizeToFit];
[urlField setFrame:CGRectMake(10,12,tableWidth-tablePadding-50,20)];
if ([self request]) {
[urlField setText:[[[self request] url] absoluteString]];
} else {
[urlField setText:@"http://allseeing-i.com"];
}
} else if ([indexPath section] == 2) {
cell = [tableView dequeueReusableCellWithIdentifier:@"ResponseCell"];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ResponseCell"] autorelease];
responseField = [[[UITextView alloc] initWithFrame:CGRectZero] autorelease];
[responseField setBackgroundColor:[UIColor clearColor]];
[[cell contentView] addSubview:responseField];
}
[responseField setFrame:CGRectMake(5,5,tableWidth-tablePadding,150)];
if (request) {
if ([request error]) {
[responseField setText:[[request error] localizedDescription]];
} else if ([request responseString]) {
[responseField setText:[request responseString]];
}
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"HeaderCell"];
if (!cell) {
cell = [DetailCell cell];
}
NSString *key = [[[request responseHeaders] allKeys] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:key];
[[cell detailTextLabel] setText:[[request responseHeaders] objectForKey:key]];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
if (section == 3) {
return [[request responseHeaders] count];
} else {
return 1;
}
}
- (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] == 1) {
return 48;
} else if ([indexPath section] == 2) {
return 160;
} else {
return 34;
}
}
- (NSString *)tableView:(UITableView *)theTableView titleForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
return nil;
case 1:
return @"Enter a URL";
case 2:
return @"Response";
case 3:
return @"Response Headers";
}
return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([self request]) {
return 4;
} else {
return 2;
}
}
@synthesize request;
@end