forked from acekiller/iOS-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvancedGetController.m
More file actions
543 lines (432 loc) · 19.6 KB
/
AdvancedGetController.m
File metadata and controls
543 lines (432 loc) · 19.6 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
File: AdvancedGetController.m
Contains: Manages the Get tab.
Written by: DTS
Copyright: Copyright (c) 2010 Apple Inc. All Rights Reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of
these terms. If you do not agree with these terms, please do
not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following
terms, and subject to these terms, Apple grants you a personal,
non-exclusive license, under Apple's copyrights in this
original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or
without modifications, in source and/or binary forms; provided
that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the
following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks
or logos of Apple Inc. may be used to endorse or promote
products derived from the Apple Software without specific prior
written permission from Apple. Except as expressly stated in
this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or
by other works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis.
APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING
THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY
OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
// IMPORTANT: This code is very similar to the code from the SimpleURLConnections
// sample except that it a) handle authentication challenges, and b) supports
// a pick list (to make testing easier). If you want a more gentle introduction
// to NSURLConnection, you should start with the SimpleURLConnections first.
#import "AdvancedGetController.h"
#import "ChallengeHandler.h"
#import "PickListController.h"
#import "DebugOptions.h"
#import "AppDelegate.h"
#pragma mark * GetController
@interface AdvancedGetController () <UITextFieldDelegate, PickListControllerDelegate, ChallengeHandlerDelegate>
// Properties that don't need to be seen by the outside world.
@property (nonatomic, retain, readwrite) PickListController * pickList;
@property (nonatomic, assign, readonly) BOOL isReceiving;
@property (nonatomic, retain, readwrite) NSURLConnection * connection;
@property (nonatomic, retain, readwrite) ChallengeHandler * currentChallenge;
@property (nonatomic, retain, readwrite) NSTimer * earlyTimeoutTimer;
@property (nonatomic, copy, readwrite) NSString * filePath;
@property (nonatomic, retain, readwrite) NSOutputStream * fileStream;
@end
@implementation AdvancedGetController
#pragma mark * Status management
// These methods are used by the core transfer code to update the UI.
- (void)_updateStatus:(NSString *)statusString
{
assert(statusString != nil);
self.statusLabel.text = statusString;
NSLog(@"status: %@", statusString);
}
- (void)_receiveDidStart
{
// Clear the current image so that we get a nice visual cue if the receive fails.
self.imageView.image = nil;
[self _updateStatus:@"Receiving"];
self.getOrCancelButton.title = @"Cancel";
[self.activityIndicator startAnimating];
[[AppDelegate sharedAppDelegate] didStartNetworking];
}
- (void)_receiveDidStopWithStatus:(NSString *)statusString
{
if (statusString == nil) {
assert(self.filePath != nil);
self.imageView.image = [UIImage imageWithContentsOfFile:self.filePath];
statusString = @"Get succeeded";
}
[self _updateStatus:statusString];
self.getOrCancelButton.title = @"Get";
[self.activityIndicator stopAnimating];
[[AppDelegate sharedAppDelegate] didStopNetworking];
}
#pragma mark * Core transfer code
// This is the code that actually does the networking.
@synthesize connection = _connection;
@synthesize currentChallenge = _currentChallenge;
@synthesize earlyTimeoutTimer = _earlyTimeoutTimer;
@synthesize filePath = _filePath;
@synthesize fileStream = _fileStream;
- (BOOL)isReceiving
{
return (self.connection != nil);
}
- (void)_startReceive
// Starts a connection to download the current URL.
{
BOOL success;
NSURL * url;
NSURLRequest * request;
assert(self.connection == nil); // don't tap receive twice in a row!
assert(self.currentChallenge == nil); // ditto
assert(self.earlyTimeoutTimer == nil); // ditto
assert(self.filePath == nil); // ditto
assert(self.fileStream == nil); // ditto
// First get and check the URL.
url = [[AppDelegate sharedAppDelegate] smartURLForString:self.urlText.text];
success = (url != nil);
// If the URL is bogus, let the user know. Otherwise kick off the connection.
if ( ! success) {
[self _updateStatus:@"Invalid URL"];
} else {
// Open a stream for the file we're going to receive into.
self.filePath = [[AppDelegate sharedAppDelegate] pathForTemporaryFileWithPrefix:@"Get"];
assert(self.filePath != nil);
self.fileStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:NO];
assert(self.fileStream != nil);
[self.fileStream open];
// Open a connection for the URL.
request = [NSURLRequest requestWithURL:url];
assert(request != nil);
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
assert(self.connection != nil);
// If we've been told to use an early timeout for debugging purposes,
// set that up now.
if ([DebugOptions sharedDebugOptions].earlyTimeout) {
self.earlyTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(_earlyTimeout:) userInfo:nil repeats:NO];
assert(self.earlyTimeoutTimer != nil);
}
// Tell the UI we're receiving.
[self _receiveDidStart];
}
}
- (void)_stopReceiveWithStatus:(NSString *)statusString
// Shuts down the connection and displays the result (statusString == nil)
// or the error status (otherwise).
{
if (self.earlyTimeoutTimer != nil) {
[self.earlyTimeoutTimer invalidate];
self.earlyTimeoutTimer = nil;
}
if (self.currentChallenge != nil) {
[self.currentChallenge stop];
self.currentChallenge = nil;
}
if (self.connection != nil) {
[self.connection cancel];
self.connection = nil;
}
if (self.fileStream != nil) {
[self.fileStream close];
self.fileStream = nil;
}
[self _receiveDidStopWithStatus:statusString];
self.filePath = nil;
}
- (void)_earlyTimeout:(NSTimer *)timer
// Called by a timer (if the earlyTimout debugging option is enabled) to
// test the code that cancels authentication challenges. This flushed out a
// scary number of bugs (-:
{
#pragma unused(timer)
assert(timer != nil);
assert(timer == self.earlyTimeoutTimer);
[self _stopReceiveWithStatus:@"Early Timeout"];
}
- (BOOL)connection:(NSURLConnection *)conn canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
// A delegate method called by the NSURLConnection when something happens with the
// connection security-wise. We defer all of the logic for how to handle this to
// the ChallengeHandler module (and it's very custom subclasses).
{
#pragma unused(conn)
BOOL result;
assert(conn == self.connection);
assert(protectionSpace != nil);
result = [ChallengeHandler supportsProtectionSpace:protectionSpace];
NSLog(@"canAuthenticateAgainstProtectionSpace %@ -> %d", [protectionSpace authenticationMethod], result);
return result;
}
- (void)connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
// A delegate method called by the NSURLConnection when you accept a specific
// authentication challenge by returning YES from -connection:canAuthenticateAgainstProtectionSpace:.
// Again, most of the logic has been shuffled off to the ChallengeHandler module; the only
// policy decision we make here is that, if the challenge handle doesn't get it right in 5 tries,
// we bail out.
{
#pragma unused(conn)
assert(conn == self.connection);
assert(challenge != nil);
NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);
assert(self.currentChallenge == nil);
if ([challenge previousFailureCount] < 5) {
self.currentChallenge = [ChallengeHandler handlerForChallenge:challenge parentViewController:self];
if (self.currentChallenge == nil) {
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
} else {
self.currentChallenge.delegate = self;
[self.currentChallenge start];
}
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
// Somewhat confusingly the -connection:didCancelAuthenticationChallenge: isn't meaningful in the
// context of an NSURLConnection, so we just don't implement that method.
- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
// A delegate method called by the NSURLConnection when the request/response
// exchange is complete. We look at the response to check that the HTTP
// status code is 2xx and that the Content-Type is acceptable. If these checks
// fail, we give up on the transfer.
{
#pragma unused(conn)
static NSSet * sSupportedImageTypes;
NSHTTPURLResponse * httpResponse;
assert(conn == self.connection);
NSLog(@"didReceiveResponse");
if (sSupportedImageTypes == nil) {
sSupportedImageTypes = [[NSSet alloc] initWithObjects:@"image/jpeg", @"image/png", @"image/gif", nil];
assert(sSupportedImageTypes != nil);
}
httpResponse = (NSHTTPURLResponse *) response;
assert( [httpResponse isKindOfClass:[NSHTTPURLResponse class]] );
if ((httpResponse.statusCode / 100) != 2) {
[self _stopReceiveWithStatus:[NSString stringWithFormat:@"HTTP error %zd", (ssize_t) httpResponse.statusCode]];
} else {
NSString * fileMIMEType;
fileMIMEType = [[httpResponse MIMEType] lowercaseString];
if (fileMIMEType == nil) {
[self _stopReceiveWithStatus:@"No Content-Type!"];
} else if ( ! [sSupportedImageTypes containsObject:fileMIMEType] ) {
[self _stopReceiveWithStatus:[NSString stringWithFormat:@"Unsupported Content-Type (%@)", fileMIMEType]];
} else {
[self _updateStatus:@"Response OK."];
}
}
}
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
// A delegate method called by the NSURLConnection as data arrives. We just
// write the data to the file.
{
#pragma unused(conn)
NSInteger dataLength;
const uint8_t * dataBytes;
NSInteger bytesWritten;
NSInteger bytesWrittenSoFar;
assert(conn == self.connection);
dataLength = [data length];
dataBytes = [data bytes];
bytesWrittenSoFar = 0;
do {
bytesWritten = [self.fileStream write:&dataBytes[bytesWrittenSoFar] maxLength:dataLength - bytesWrittenSoFar];
assert(bytesWritten != 0);
if (bytesWritten == -1) {
[self _stopReceiveWithStatus:@"File write error"];
break;
} else {
bytesWrittenSoFar += bytesWritten;
}
} while (bytesWrittenSoFar != dataLength);
}
- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
// A delegate method called by the NSURLConnection if the connection fails.
// We shut down the connection and display the failure. Production quality code
// would either display or log the actual error.
{
#pragma unused(conn)
#pragma unused(error)
assert(conn == self.connection);
NSLog(@"didFailWithError %@", error);
[self _stopReceiveWithStatus:@"Connection failed"];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)conn
// A delegate method called by the NSURLConnection when the connection has been
// done successfully. We shut down the connection with a nil status, which
// causes the image to be displayed.
{
#pragma unused(conn)
assert(conn == self.connection);
NSLog(@"connectionDidFinishLoading");
[self _stopReceiveWithStatus:nil];
}
#pragma mark * Authentication challenge UI
- (void)challengeHandlerDidFinish:(ChallengeHandler *)handler
// Called by the authentication challenge handler once the challenge is
// resolved. We twiddle our internal state and then call the -resolve method
// to apply the challenge results to the NSURLAuthenticationChallenge.
{
#pragma unused(handler)
ChallengeHandler * challenge;
assert(handler == self.currentChallenge);
// We want to nil out currentChallenge because we've really done with this
// challenge now and, for example, if the next operation kicks up a new
// challenge, we want to make sure that currentChallenge is ready to receive
// it.
//
// We want the challenge to hang around after we've nilled out currentChallenge,
// so retain/autorelease it.
challenge = [[self.currentChallenge retain] autorelease];
self.currentChallenge = nil;
// If the credential isn't present, this will trigger a -connection:didFailWithError:
// callback.
NSLog(@"resolve %@ -> %@", [[challenge.challenge protectionSpace] authenticationMethod], challenge.credential);
[challenge resolve];
}
#pragma mark * UI Actions
@synthesize pickList = _pickList;
- (void)textFieldDidBeginEditing:(UITextField *)textField
// A delegate method called by the URL text field when the user start editing
// the field. We respond by bringing up our pick list.
{
#pragma unused(textField)
assert(textField == self.urlText);
assert(self.pickList == nil);
self.pickList = [[[PickListController alloc] initWithPickListNamed:@"AdvancedGetControllerPickList" bundle:nil] autorelease];
assert(self.pickList != nil);
self.pickList.delegate = self;
[self.pickList attachBelowView:self.toolbar];
}
- (void)pickList:(PickListController *)controller didPick:(NSString *)picked
// A delegate method called by our pick list controller when the user picks
// an entry. We stash the entry into our URL text field (it will get saved
// by -textFieldDidEndEditing:, below), dismiss the keyboard (which tears
// down the pick list, again in -textFieldDidEndEditing:), and then
// start the transfer.
{
#pragma unused(controller)
assert(controller == self.pickList);
assert(picked != nil);
self.urlText.text = picked;
[self.urlText resignFirstResponder];
if (self.isReceiving) {
[self _stopReceiveWithStatus:@"Cancelled"];
}
[self _startReceive];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
// A delegate method called by the URL text field when the user taps the Return
// key. We just dismiss the keyboard (which has the side effect of tearing
// down the pick list per the code in -textFieldDidEndEditing:).
{
#pragma unused(textField)
assert(textField == self.urlText);
[self.urlText resignFirstResponder];
return NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
// A delegate method called by the URL text field when the editing is complete.
// We dismiss the pick list and save the current value of the field in our settings.
{
#pragma unused(textField)
NSString * newValue;
NSString * oldValue;
assert(textField == self.urlText);
assert(self.pickList != nil);
[self.pickList detach];
self.pickList = nil;
newValue = self.urlText.text;
oldValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"GetURLText"];
// Save the URL text if it's changed.
assert(newValue != nil); // what is UITextField thinking!?!
assert(oldValue != nil); // because we registered a default
if ( ! [newValue isEqual:oldValue] ) {
[[NSUserDefaults standardUserDefaults] setObject:newValue forKey:@"GetURLText"];
}
}
- (IBAction)getOrCancelAction:(id)sender
// Called when the user taps the Get button in the toolbar (which is a Cancel
// button if we're transferring).
{
#pragma unused(sender)
[self.urlText resignFirstResponder];
if (self.isReceiving) {
[self _stopReceiveWithStatus:@"Cancelled"];
} else {
[self _startReceive];
}
}
#pragma mark * View controller boilerplate
@synthesize urlText = _urlText;
@synthesize imageView = _imageView;
@synthesize statusLabel = _statusLabel;
@synthesize activityIndicator = _activityIndicator;
@synthesize getOrCancelButton = _getOrCancelButton;
@synthesize toolbar = _toolbar;
- (void)viewDidLoad
{
[super viewDidLoad];
assert(self.urlText != nil);
assert(self.urlText.delegate == self);
assert(self.imageView != nil);
assert(self.statusLabel != nil);
assert(self.activityIndicator != nil);
assert(self.getOrCancelButton != nil);
assert(self.toolbar != nil);
self.getOrCancelButton.possibleTitles = [NSSet setWithObjects:@"Get", @"Cancel", nil];
self.urlText.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"GetURLText"];
self.activityIndicator.hidden = YES;
[self _updateStatus:@""];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.urlText = nil;
self.imageView = nil;
self.statusLabel = nil;
self.activityIndicator = nil;
self.getOrCancelButton = nil;
self.toolbar = nil;
}
- (void)dealloc
{
[self _stopReceiveWithStatus:@"Stopped"];
[self->_urlText release];
[self->_imageView release];
[self->_statusLabel release];
[self->_activityIndicator release];
[self->_getOrCancelButton release];
[self->_toolbar release];
[super dealloc];
}
@end