root / asi-http-request-with-pithos / iPhone Sample / UploadViewController.m @ cc176feb
History | View | Annotate | Download (6.6 kB)
1 |
// |
---|---|
2 |
// UploadViewController.m |
3 |
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details |
4 |
// |
5 |
// Created by Ben Copsey on 31/12/2008. |
6 |
// Copyright 2008 All-Seeing Interactive. All rights reserved. |
7 |
// |
8 |
|
9 |
#import "UploadViewController.h" |
10 |
#import "ASIFormDataRequest.h" |
11 |
#import "InfoCell.h" |
12 |
|
13 |
// Private stuff |
14 |
@interface UploadViewController () |
15 |
- (void)uploadFailed:(ASIHTTPRequest *)theRequest; |
16 |
- (void)uploadFinished:(ASIHTTPRequest *)theRequest; |
17 |
@end |
18 |
|
19 |
@implementation UploadViewController |
20 |
|
21 |
- (IBAction)performLargeUpload:(id)sender |
22 |
{ |
23 |
[request cancel]; |
24 |
[self setRequest:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ignore"]]]; |
25 |
[request setPostValue:@"test" forKey:@"value1"]; |
26 |
[request setPostValue:@"test" forKey:@"value2"]; |
27 |
[request setPostValue:@"test" forKey:@"value3"]; |
28 |
[request setTimeOutSeconds:20]; |
29 |
|
30 |
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 |
31 |
[request setShouldContinueWhenAppEntersBackground:YES]; |
32 |
#endif |
33 |
[request setUploadProgressDelegate:progressIndicator]; |
34 |
[request setDelegate:self]; |
35 |
[request setDidFailSelector:@selector(uploadFailed:)]; |
36 |
[request setDidFinishSelector:@selector(uploadFinished:)]; |
37 |
|
38 |
//Create a 256KB file |
39 |
NSData *data = [[[NSMutableData alloc] initWithLength:256*1024] autorelease]; |
40 |
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"file"]; |
41 |
[data writeToFile:path atomically:NO]; |
42 |
|
43 |
//Add the file 8 times to the request, for a total request size around 2MB |
44 |
int i; |
45 |
for (i=0; i<8; i++) { |
46 |
[request setFile:path forKey:[NSString stringWithFormat:@"file-%i",i]]; |
47 |
} |
48 |
|
49 |
[request startAsynchronous]; |
50 |
[resultView setText:@"Uploading data..."]; |
51 |
} |
52 |
|
53 |
- (IBAction)toggleThrottling:(id)sender |
54 |
{ |
55 |
[ASIHTTPRequest setShouldThrottleBandwidthForWWAN:[(UISwitch *)sender isOn]]; |
56 |
} |
57 |
|
58 |
- (void)uploadFailed:(ASIHTTPRequest *)theRequest |
59 |
{ |
60 |
[resultView setText:[NSString stringWithFormat:@"Request failed:\r\n%@",[[theRequest error] localizedDescription]]]; |
61 |
} |
62 |
|
63 |
- (void)uploadFinished:(ASIHTTPRequest *)theRequest |
64 |
{ |
65 |
[resultView setText:[NSString stringWithFormat:@"Finished uploading %llu bytes of data",[theRequest postLength]]]; |
66 |
|
67 |
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 |
68 |
// Clear out the old notification before scheduling a new one. |
69 |
if ([[[UIApplication sharedApplication] scheduledLocalNotifications] count] > 0) |
70 |
[[UIApplication sharedApplication] cancelAllLocalNotifications]; |
71 |
|
72 |
// Create a new notification |
73 |
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; |
74 |
if (notification) { |
75 |
[notification setFireDate:[NSDate date]]; |
76 |
[notification setTimeZone:[NSTimeZone defaultTimeZone]]; |
77 |
[notification setRepeatInterval:0]; |
78 |
[notification setSoundName:@"alarmsound.caf"]; |
79 |
[notification setAlertBody:@"Boom!\r\n\r\nUpload is finished!"]; |
80 |
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; |
81 |
} |
82 |
#endif |
83 |
} |
84 |
|
85 |
- (void)dealloc |
86 |
{ |
87 |
[request setDelegate:nil]; |
88 |
[request setUploadProgressDelegate:nil]; |
89 |
[request cancel]; |
90 |
[request release]; |
91 |
[progressIndicator release]; |
92 |
[resultView release]; |
93 |
[super dealloc]; |
94 |
} |
95 |
|
96 |
/* |
97 |
Most of the code below here relates to the table view, and isn't that interesting |
98 |
*/ |
99 |
|
100 |
- (void)viewDidLoad |
101 |
{ |
102 |
[super viewDidLoad]; |
103 |
[[[self navigationBar] topItem] setTitle:@"Tracking Upload Progress"]; |
104 |
resultView = [[UITextView alloc] initWithFrame:CGRectZero]; |
105 |
[resultView setBackgroundColor:[UIColor clearColor]]; |
106 |
progressIndicator = [[UIProgressView alloc] initWithFrame:CGRectZero]; |
107 |
} |
108 |
|
109 |
static NSString *intro = @"Demonstrates POSTing content to a URL, showing upload progress.\nYou'll only see accurate progress for uploads when the request body is larger than 128KB (in 2.2.1 SDK), or when the request body is larger than 32KB (in 3.0 SDK)\n\nThis request is also setup to run when the app enters the background on devices running on iOS4. In the delegate method that is called when the request finishes, we show a local notification to let the user know the upload is finished."; |
110 |
|
111 |
- (UIView *)tableView:(UITableView *)theTableView viewForHeaderInSection:(NSInteger)section |
112 |
{ |
113 |
if (section == 1) { |
114 |
int tablePadding = 40; |
115 |
int tableWidth = [tableView frame].size.width; |
116 |
if (tableWidth > 480) { // iPad |
117 |
tablePadding = 110; |
118 |
} |
119 |
|
120 |
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableWidth-(tablePadding/2),30)] autorelease]; |
121 |
UIButton *goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
122 |
[goButton setTitle:@"Go!" forState:UIControlStateNormal]; |
123 |
[goButton sizeToFit]; |
124 |
[goButton setFrame:CGRectMake([view frame].size.width-[goButton frame].size.width+10,7,[goButton frame].size.width,[goButton frame].size.height)]; |
125 |
|
126 |
[goButton addTarget:self action:@selector(performLargeUpload:) forControlEvents:UIControlEventTouchUpInside]; |
127 |
[view addSubview:goButton]; |
128 |
[progressIndicator setFrame:CGRectMake((tablePadding/2)-10,20,200,10)]; |
129 |
[view addSubview:progressIndicator]; |
130 |
|
131 |
return view; |
132 |
} |
133 |
return nil; |
134 |
} |
135 |
|
136 |
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
137 |
{ |
138 |
int tablePadding = 40; |
139 |
int tableWidth = [tableView frame].size.width; |
140 |
if (tableWidth > 480) { // iPad |
141 |
tablePadding = 110; |
142 |
} |
143 |
|
144 |
UITableViewCell *cell; |
145 |
if ([indexPath section] == 0) { |
146 |
cell = [tableView dequeueReusableCellWithIdentifier:@"InfoCell"]; |
147 |
if (!cell) { |
148 |
cell = [InfoCell cell]; |
149 |
} |
150 |
[[cell textLabel] setText:intro]; |
151 |
[cell layoutSubviews]; |
152 |
|
153 |
|
154 |
} else if ([indexPath section] == 1) { |
155 |
return nil; |
156 |
} else { |
157 |
cell = [tableView dequeueReusableCellWithIdentifier:@"Response"]; |
158 |
if (!cell) { |
159 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Response"] autorelease]; |
160 |
[[cell contentView] addSubview:resultView]; |
161 |
} |
162 |
[resultView setFrame:CGRectMake(5,5,tableWidth-tablePadding,60)]; |
163 |
} |
164 |
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
165 |
|
166 |
return cell; |
167 |
} |
168 |
|
169 |
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section |
170 |
{ |
171 |
if (section == 1) { |
172 |
return 0; |
173 |
} |
174 |
return 1; |
175 |
} |
176 |
|
177 |
- (CGFloat)tableView:(UITableView *)theTableView heightForHeaderInSection:(NSInteger)section |
178 |
{ |
179 |
if (section == 1) { |
180 |
return 50; |
181 |
} |
182 |
return 34; |
183 |
} |
184 |
|
185 |
- (CGFloat)tableView:(UITableView *)theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath |
186 |
{ |
187 |
if ([indexPath section] == 0) { |
188 |
return [InfoCell neededHeightForDescription:intro withTableWidth:[tableView frame].size.width]+20; |
189 |
} else if ([indexPath section] == 2) { |
190 |
return 80; |
191 |
} else { |
192 |
return 42; |
193 |
} |
194 |
} |
195 |
|
196 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView |
197 |
{ |
198 |
return 3; |
199 |
} |
200 |
|
201 |
@synthesize request; |
202 |
@end |