-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebbrowserViewController.m
621 lines (543 loc) · 22.9 KB
/
WebbrowserViewController.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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//
// WebbrowserViewController.m
// Nautical
//
// Created by Helge Städtler on 10.01.12.
// Copyright (c) 2012 appdoctors. All rights reserved.
//
#import "WebbrowserViewController.h"
#import <MobileCoreServices/MobileCoreServices.h> // UIPasteboard stuff
#import "SinaURLConnection.h"
#define kSTRING_DEFAULT_LOADING @"Laden..."
#define kSTRING_DEFAULT_WAIT @"Warten..."
#define kSTRING_DEFAULT_ERROR @"Fehler"
#define kSTRING_DEFAULT_DONE @"Webseite"
#define kSTRING_DEFAULT_FAIL_404 @"Fehler 404"
@implementation WebbrowserViewController
@synthesize fullWebView;
@synthesize myToolBar;
@synthesize backButton;
@synthesize forwardButton;
@synthesize stopReloadButton;
@synthesize shouldScaleToFit;
@synthesize urlToOpen;
@synthesize loadingIndicator;
@synthesize delegate;
@synthesize myNavItem;
@synthesize myNavBar;
@synthesize isDisplayingNice404;
@synthesize noDataImageView;
@synthesize shouldDetectPages404;
@synthesize requestChecked;
@synthesize requestToCheck;
@synthesize shouldStartLoadingCheckedRequest;
@synthesize isRequestCheckDone;
@synthesize timerActivityIndicator;
@synthesize shouldAddDoneButton;
@synthesize shouldAddActionButton;
@synthesize shouldUseSmoothFading;
@synthesize timerFadeInFinal;
@synthesize isPresentedModal;
@synthesize currentRequest;
BOOL isFirstLoad = YES;
BOOL isShowingActionSheet = NO;
#pragma mark -
#pragma mark destruction
- (void)dealloc {
self.delegate = nil;
if( timerActivityIndicator && [timerActivityIndicator isValid] ) {
[timerActivityIndicator invalidate];
}
self.timerActivityIndicator = nil;
if( timerFadeInFinal && [timerFadeInFinal isValid] ) {
[timerFadeInFinal invalidate];
}
self.timerFadeInFinal = nil;
[fullWebView release];
[myToolBar release];
[backButton release];
[forwardButton release];
[stopReloadButton release];
[urlToOpen release];
[loadingIndicator release];
self.myNavItem = nil;
self.myNavBar = nil;
self.noDataImageView = nil;
self.requestChecked = nil;
self.requestToCheck =nil;
[super dealloc];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void) shouldShowNetworkIndicator {
}
- (void) shouldHideNetworkIndicator {
}
#pragma mark - Activity Timer Handling
- (void) resetTimerForFadeOutActivityIndicator {
if( timerActivityIndicator && [timerActivityIndicator isValid] ) {
[timerActivityIndicator invalidate];
}
self.timerActivityIndicator = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(fadeOutActivityIndicator) userInfo:nil repeats:NO];
}
- (void) fadeInActivityIndicator {
if( !loadingIndicator.isAnimating ) {
[loadingIndicator startAnimating];
loadingIndicator.alpha = 0.0;
[UIView animateWithDuration:0.5 animations:^{
loadingIndicator.alpha = 1.0;
} completion:^(BOOL finished) {
[self switchToStop];
[self resetTimerForFadeOutActivityIndicator];
}];
}
else {
[self resetTimerForFadeOutActivityIndicator];
}
}
- (void) fadeOutActivityIndicator {
loadingIndicator.alpha = 1.0;
[UIView animateWithDuration:0.5 animations:^{
loadingIndicator.alpha = 0.0;
} completion:^(BOOL finished) {
loadingIndicator.alpha = 0.0;
[loadingIndicator stopAnimating];
[self switchToReload];
}];
}
- (void) startFadeInTimer {
self.timerFadeInFinal = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(actionFadeInWebView) userInfo:nil repeats:NO];
}
#pragma mark - construction
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.shouldScaleToFit = YES;
self.isDisplayingNice404 = NO;
self.shouldDetectPages404 = NO;
self.isRequestCheckDone = NO;
self.shouldAddDoneButton = YES;
self.shouldAddActionButton = YES;
self.shouldUseSmoothFading = NO;
}
return self;
}
- (void) addUserAgentInfoToRequest:(NSMutableURLRequest*)request {
NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSString *appPlatform = [[UIDevice currentDevice] platformString];
NSString *appSystemVersion = [[UIDevice currentDevice] systemVersion];
NSString *appLanguageContext = [[NSLocale currentLocale] localeIdentifier];
NSString *uaString = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; %@)", appName, appVersion, appPlatform, appSystemVersion, appLanguageContext];
if( DEBUG ) NSLog( @"USER AGENT: %@", uaString );
[request setValue:uaString forHTTPHeaderField:@"User-Agent"];
}
- (void) actionFadeInWebView {
if( !shouldUseSmoothFading ) {
fullWebView.alpha = 1.0f;
return;
}
if( fullWebView.alpha != 0.0f ) return;
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
fullWebView.alpha = 1.0f;
} completion:^(BOOL finished) {
// do nothing
}];
}
- (void) addActionButtonToNavigationItem:(UINavigationItem*)navigationItemToAddTo animated:(BOOL)animated {
if( !navigationItemToAddTo ) return;
UIBarButtonItem *actionButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionAskForAction)] autorelease];
[navigationItemToAddTo setRightBarButtonItem:actionButton animated:animated];
}
- (void) addDoneButtonToNavigationItem:(UINavigationItem*)navigationItemToAddTo animated:(BOOL)animated {
if( !navigationItemToAddTo ) return;
UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actionDone:)] autorelease];
[navigationItemToAddTo setLeftBarButtonItem:doneButton animated:NO];
}
- (void)viewDidLoad {
[super viewDidLoad];
if( !isPresentedModal ) {
[myNavBar removeFromSuperview];
self.myNavItem = nil;
self.myNavBar = nil;
self.myNavItem = self.navigationItem;
self.myNavBar = self.navigationController.navigationBar;
}
myNavItem.title = kSTRING_DEFAULT_LOADING;
loadingIndicator.alpha = 0.0f;
if( shouldAddDoneButton ) {
[self addDoneButtonToNavigationItem:myNavItem animated:NO];
}
if( shouldAddActionButton ) {
[self addActionButtonToNavigationItem:myNavItem animated:NO];
}
if( shouldUseSmoothFading ) {
[self startFadeInTimer];
fullWebView.alpha = 0.0;
}
[myToolBar setTintColor:kCOLOR_BACK];
[fullWebView setScalesPageToFit:shouldScaleToFit];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlToOpen cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:30];
self.isRequestCheckDone = NO;
self.requestChecked = nil;
[self addUserAgentInfoToRequest:request];
self.requestToCheck = request;
[fullWebView loadRequest:request];
[myNavBar setTintColor:kCOLOR_BACK];
/*
NSMutableDictionary *titleTextAttributesDict = [NSMutableDictionary dictionary];
[titleTextAttributesDict setObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[titleTextAttributesDict setObject:[UIFont fontWithName:@"Cabin-Bold" size:24.0] forKey:UITextAttributeFont];
myNavBar.titleTextAttributes = titleTextAttributesDict;
*/
noDataImageView.hidden = YES;
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
switch ( actionSheet.tag ) {
case kACTIONSHEET_WEBVIEW_ASK_ACTION:
if( buttonIndex == actionSheet.firstOtherButtonIndex ) {
[UIPasteboard generalPasteboard].string = [urlToOpen absoluteString];
}
if( buttonIndex == actionSheet.firstOtherButtonIndex+1 ) {
[[UIApplication sharedApplication] openURL:urlToOpen];
}
break;
default:
break;
}
isShowingActionSheet = NO;
}
#pragma mark - user interaction
- (void) cancelPendingRequestsBeforeExit {
fullWebView.delegate = nil;
[fullWebView stopLoading];
}
- (IBAction) actionStopReload:(id)sender {
if( fullWebView.isLoading ) {
[fullWebView stopLoading];
}
else {
[fullWebView reload];
}
}
- (IBAction) actionDone:(id)sender {
[self cancelPendingRequestsBeforeExit];
if( isPresentedModal ) {
if( delegate && [delegate respondsToSelector:@selector(webcontentViewControllerDidFinish:)] ) {
[delegate webcontentViewControllerDidFinish:self];
}
else { // REMOVE INELEGANTLY
[self.view.superview removeFromSuperview];
}
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
// TODO: exchange standard actionsheet with block actionsheet
/*
BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Alert Title" message:@"This is a very long message, designed just to show you how smart this class is"];
[alert setCancelButtonWithTitle:@"Cancel" block:nil];
[alert setDestructiveButtonWithTitle:@"Kill!" block:nil];
[alert addButtonWithTitle:@"Show Action Sheet on top" block:^{
[self showActionSheet:nil];
}];
[alert addButtonWithTitle:@"Show another alert" block:^{
[self showAlert:nil];
}];
[alert show];
*/
- (IBAction) actionAskForAction {
if( isShowingActionSheet ) return;
NSString *shortUrl = [urlToOpen absoluteString];
if( [shortUrl length] > 40 ) {
shortUrl = [shortUrl substringToIndex:40];
shortUrl = [shortUrl stringByAppendingString:@" […]"];
}
NSString *sheetTitle = [NSString stringWithFormat:@"%@\n%@",myNavItem.title, shortUrl ];
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:sheetTitle delegate:self cancelButtonTitle:LOC(@"Abbrechen") destructiveButtonTitle:nil otherButtonTitles:LOC(@"URL kopieren"),LOC(@"Öffnen in Safari"), nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
sheet.tag = kACTIONSHEET_WEBVIEW_ASK_ACTION;
isShowingActionSheet = YES;
if( [sheet respondsToSelector:@selector(showFromBarButtonItem:)] ) {
[sheet showFromBarButtonItem:myNavItem.rightBarButtonItem animated:YES];
}
else {
[sheet showInView:self.view];
}
[sheet release];
}
#pragma mark - webview management
- (void) switchToReload {
NSArray *toobarItems = myToolBar.items;
NSMutableArray *itemsFresh = [NSMutableArray array];
for( UIBarButtonItem *currentItem in toobarItems ) {
if( currentItem != stopReloadButton ) {
[itemsFresh addObject:currentItem];
}
else {
self.stopReloadButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:fullWebView action:@selector(reload)] autorelease];
[itemsFresh addObject:stopReloadButton];
}
}
[myToolBar setItems:itemsFresh animated:YES];
}
- (void) switchToStop {
NSArray *toobarItems = myToolBar.items;
NSMutableArray *itemsFresh = [NSMutableArray array];
for( UIBarButtonItem *currentItem in toobarItems ) {
if( currentItem != stopReloadButton ) {
[itemsFresh addObject:currentItem];
}
else {
self.stopReloadButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:fullWebView action:@selector(stopLoading)] autorelease];
[itemsFresh addObject:stopReloadButton];
}
}
[myToolBar setItems:itemsFresh animated:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self shouldHideNetworkIndicator];
[backButton setEnabled:[webView canGoBack]];
[forwardButton setEnabled:[webView canGoForward]];
if( fullWebView.alpha == 0.0f ) {
[self actionFadeInWebView];
}
if( !isFirstLoad ) {
NSString *documentTitle = [fullWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSString *documentBaseUrl = [fullWebView stringByEvaluatingJavaScriptFromString:@"document.URL"];
if( documentBaseUrl && [documentBaseUrl length] > 0 ) {
self.urlToOpen = [NSURL URLWithString:documentBaseUrl];
}
// NSString *htmlInBody = [fullWebView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
if( documentTitle && [documentTitle length] > 0 ) {
myNavItem.title = documentTitle;
}
else {
myNavItem.title = kSTRING_DEFAULT_DONE;
}
}
else {
NSString *documentTitle = [fullWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSString *documentBaseUrl = [fullWebView stringByEvaluatingJavaScriptFromString:@"document.URL"];
if( documentBaseUrl && [documentBaseUrl length] > 0 ) {
self.urlToOpen = [NSURL URLWithString:documentBaseUrl];
}
// NSString *htmlInBody = [fullWebView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
if( documentTitle && [documentTitle length] > 0 ) {
myNavItem.title = documentTitle;
}
else {
myNavItem.title = kSTRING_DEFAULT_DONE;
}
isFirstLoad = NO;
}
}
- (void) showNice404 {
myNavItem.title = kSTRING_DEFAULT_FAIL_404;
noDataImageView.alpha = 0.0f;
noDataImageView.hidden = NO;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
noDataImageView.alpha = 1.0f;
} completion:^(BOOL finished) {
self.isDisplayingNice404 = YES;
}];
}
- (void) hideNice404 {
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
noDataImageView.alpha = 0.0f;
} completion:^(BOOL finished) {
noDataImageView.hidden = YES;
self.isDisplayingNice404 = NO;
myNavItem.title = kSTRING_DEFAULT_LOADING;
}];
}
- (void) continueStartLoadingRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if( !shouldStartLoadingCheckedRequest ) {
NSLog( @"SHOWING 404 FOR FAILED URL %@", [request.URL absoluteString] );
[self showNice404];
}
else {
NSLog( @"INIT REQUEST AGAIN AFTER PRECHECK %@", [request.URL absoluteString] );
[fullWebView loadRequest:request];
}
}
- (NSString*) stringStrippedByTrailingSlash:(NSString*)stringToClean {
if( !stringToClean ) return stringToClean;
while( stringToClean && ( [stringToClean length] > 0 ) && [[stringToClean substringFromIndex:[stringToClean length]-1] isEqualToString:@"/"] ) {
stringToClean = [stringToClean substringToIndex:[stringToClean length]-1];
}
return stringToClean;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// PRE CHECK DEDICATED REQUESTS WITH PREFLIGHT BEFORE LOADING
if( isRequestCheckDone ) {
shouldDetectPages404 = NO;
}
/*
NSLog( @" request.URL: %@", request.URL ? [request.URL absoluteURL] : @"NO" );
NSLog( @"requestToCheck.URL: %@", requestToCheck.URL ? [requestToCheck.URL absoluteURL] : @"NO" );
NSLog( @"requestChecked.URL: %@", requestChecked.URL ? [requestChecked.URL absoluteURL] : @"NO" );
*/
NSString *cleanRequestURL = [self stringStrippedByTrailingSlash:[request.URL absoluteString]];
NSString *cleanRequestToCheck = [self stringStrippedByTrailingSlash:[requestToCheck.URL absoluteString]];
NSString *cleanRequestChecked = [self stringStrippedByTrailingSlash:[requestChecked.URL absoluteString]];
BOOL isRequestEqualToRequestToCheck = [cleanRequestURL isEqualToString:cleanRequestToCheck];
BOOL isRequestEqualToRequestChecked = [cleanRequestURL isEqualToString:cleanRequestChecked];
/*
NSLog( @" request.URL: %@", cleanRequestURL ? cleanRequestURL : @"NO" );
NSLog( @"requestToCheck.URL: %@", cleanRequestToCheck ? cleanRequestToCheck : @"NO" );
NSLog( @"requestChecked.URL: %@", cleanRequestChecked ? cleanRequestChecked : @"NO" );
*/
if( shouldDetectPages404 && ( isRequestEqualToRequestToCheck && !isRequestEqualToRequestChecked ) ) {
NSLog( @"PRECHECKING URL %@", [request.URL absoluteString] );
self.shouldStartLoadingCheckedRequest = NO;
self.requestChecked = request;
[SinaURLConnection asyncConnectionWithRequest:request completionBlock:^(NSData *data, NSURLResponse *response) {
NSInteger httpStatusCode = ((NSHTTPURLResponse*)response).statusCode;
NSLog( @"HTTP STATUS: %i", httpStatusCode );
if( httpStatusCode < 400 ) {
self.isRequestCheckDone = YES;
[self performSelector:@selector(actionFadeInWebView) withObject:nil afterDelay:3.5];
[self hideNice404];
self.urlToOpen = request.URL;
NSLog( @"PRECHECK LOADING URL %@", [request.URL absoluteString] );
self.shouldStartLoadingCheckedRequest = YES;
[self continueStartLoadingRequest:requestChecked navigationType:navigationType];
}
else {
NSLog( @"ABORTING LOADING OF URL %@", [request.URL absoluteString] );
self.shouldStartLoadingCheckedRequest = NO;
[self continueStartLoadingRequest:requestChecked navigationType:navigationType];
}
} errorBlock:^(NSError *error) {
self.isRequestCheckDone = YES;
self.shouldStartLoadingCheckedRequest = NO;
[self continueStartLoadingRequest:requestChecked navigationType:navigationType];
} uploadProgressBlock:^(float progress) {
// do nothing
} downloadProgressBlock:^(float progress) {
// TODO: UPDATE PROGRESS DISPLAY ...
} cancelBlock:^(float progress) {
// do nothing
}];
return NO;
}
else {
NSLog( @"FINALLY LOADING URL %@", [request.URL absoluteString] );
self.shouldStartLoadingCheckedRequest = NO;
self.requestChecked = nil;
return YES;
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
// myNavItem.title = kSTRING_DEFAULT_WAIT;
[self fadeInActivityIndicator];
[backButton setEnabled:[webView canGoBack]];
[forwardButton setEnabled:[webView canGoForward]];
[self shouldShowNetworkIndicator];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
// myNavItem.title = kSTRING_DEFAULT_ERROR;
NSLog( @"WEB URL LOADING ERROR: %@", error );
[backButton setEnabled:[webView canGoBack]];
[forwardButton setEnabled:[webView canGoForward]];
[self shouldHideNetworkIndicator];
// CHECK IF THAT IS AN SSL URL
/*
// TAKEN OUT DESTROYS VIDEO STREAMS
if( [[webView.request.URL absoluteString] containsString:@"https://" ignoringCase:YES] ) {
[self connectUsingAsiRequestWithUrlString:[webView.request.URL absoluteString]];
}
*/
}
#pragma mark - ASI STUFF
- (void) addUserAgentInfoToASIRequest:(ASIHTTPRequest*)request {
NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString *appPlatform = [[UIDevice currentDevice] platformString];
NSString *appSystemVersion = [[UIDevice currentDevice] systemVersion];
NSString *appLanguageContext = [[NSLocale currentLocale] localeIdentifier];
NSString *appDisplayName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSString *uaString = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; %@)", appDisplayName, appVersion, appPlatform, appSystemVersion, appLanguageContext];
if( DEBUG ) NSLog( @"CONNECTION: USER AGENT: %@", uaString );
[request addRequestHeader:@"User-Agent" value:uaString];
request.useCookiePersistence = NO;
}
- (ASIHTTPRequest*) requestWithTimeoutInterval:(NSTimeInterval)timeoutInterval forUrl:(NSURL*)connectUrl {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:connectUrl];
self.currentRequest = request;
request.timeOutSeconds = timeoutInterval;
[self addUserAgentInfoToASIRequest:request];
return request;
}
- (ASIHTTPRequest*)requestForAbsoluteUrl:(NSString *)absoluteUrl withTimeoutInterval:(NSTimeInterval)timeoutInterval {
NSURL *url = [NSURL URLWithString:absoluteUrl];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
self.currentRequest = request;
request.timeOutSeconds = timeoutInterval;
[self addUserAgentInfoToASIRequest:request];
return request;
}
- (void) connectUsingAsiRequestWithUrlString:(NSString*)urlString {
NSURL *connectUrl = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [self requestWithTimeoutInterval:60.0f forUrl:connectUrl];
request.shouldRedirect = YES; // FOLLOW REDIRECTS (e.g. for secure connections)
request.validatesSecureCertificate = NO;
request.delegate = self;
[request startAsynchronous]; // FROM NOW ON ASI is in Control via its delegates
}
#pragma mark - ASIHTTPRequestDelegate
- (void)requestStarted:(ASIHTTPRequest *)request {
if( DEBUG ) NSLog( @"ASI: requestStarted: %@", [request.url absoluteString] );
}
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders {
}
/*
- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL {
NSLog( @"ASI: willRedirectToURL: %@",newURL );
}
*/
- (void)requestRedirected:(ASIHTTPRequest *)request {
}
- (void)requestFinished:(ASIHTTPRequest *)request {
self.currentRequest.delegate = nil;
if( DEBUG ) NSLog( @"ASI: requestFinished" );
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if( DEBUG ) NSLog( @"RESPONSE CODE: %i", request.responseStatusCode );
if( request.responseData == nil && request.responseString == nil ) {
if( DEBUG ) NSLog( @"ATTENTION: NO RESPONSE DATA. RESPONSE WAS PROBABLY NOT OF PROTOCOL 'HTTP'" );
}
NSString *pageContent = nil;
if( request.responseStatusCode < 500 ) { // SUCCESS: SOME RESULT
@try {
pageContent = request.responseString;
}
@catch (NSException *exception) {
if( DEBUG ) NSLog( @"FATAL ERROR: %s ---> %@", __PRETTY_FUNCTION__, exception );
}
}
else { // INVALID: NO RESULT
if( DEBUG ) NSLog( @"ERROR RESPONSE CODE: %i", request.responseStatusCode );
}
if( pageContent ) { // LOAD INTO WEBVIEW NOW
[fullWebView loadHTMLString:pageContent baseURL:request.url];
}
}
- (void)requestFailed:(ASIHTTPRequest *)request {
self.currentRequest.delegate = nil;
if( DEBUG ) NSLog( @"ASI: requestFailed" );
if( DEBUG ) NSLog( @"RESPONSE CODE: %i", request.responseStatusCode );
}
@end