-
Notifications
You must be signed in to change notification settings - Fork 1
/
TLWebViewController.m
268 lines (225 loc) · 9.48 KB
/
TLWebViewController.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
//
// TLWebViewController.m
// TLCommon
//
// Created by Joshua Bleecher Snyder on 10/27/09.
//
#import "TLWebViewController.h"
#import "TLMacros.h"
#import "UIWebView_TLCommon.h"
#import "UIBarButtonItem_TLCommon.h"
#import "CGGeometry_TLCommon.h"
#import "UIImage_TLCommon.h"
#import "UIApplication_TLCommon.h"
#import "UIAlertView_TLCommon.h"
#pragma mark -
@interface TLWebViewController ()
- (void)refreshToolbars;
- (void)systemAction:(UIBarButtonItem *)sender;
@property(nonatomic, retain, readwrite) UIWebView *webView;
@property(nonatomic, assign, readwrite) BOOL loading;
@property(nonatomic, assign, readwrite) BOOL toolbarWasHidden;
@end
#pragma mark -
@implementation TLWebViewController
@synthesize webView;
@synthesize loading;
@synthesize toolbarWasHidden;
- (void)loadView {
[super loadView];
self.webView = [[[UIWebView alloc] initWithFrame:CGRectZeroWithSize(self.view.bounds.size)] autorelease];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.webView.delegate = self;
[self.view addSubview:self.webView];
}
- (void)loadRequest:(NSURLRequest *)request {
[self.webView loadRequest:request];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.toolbarWasHidden = self.navigationController.toolbarHidden;
self.navigationController.toolbarHidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.toolbarHidden = self.toolbarWasHidden;
}
- (void)viewDidUnload {
[self.webView stopLoading];
self.webView.delegate = nil;
self.webView = nil;
}
- (void)refreshToolbars {
UIBarButtonItem *back = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageWithName:@"web_back"]
style:UIBarButtonItemStylePlain
target:self.webView
action:@selector(goBack)]
autorelease];
back.enabled = self.webView.canGoBack;
UIBarButtonItem *forward = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageWithName:@"web_forward"]
style:UIBarButtonItemStylePlain
target:self.webView
action:@selector(goForward)]
autorelease];
forward.enabled = self.webView.canGoForward;
UIBarButtonSystemItem stopOrRefreshSystemItem = self.webView.loading ? UIBarButtonSystemItemStop : UIBarButtonSystemItemRefresh;
SEL stopOrRefreshAction = self.webView.loading ? @selector(stopLoading) : @selector(reload);
UIBarButtonItem *stopOrRefresh = [UIBarButtonItem barButtonSystemItem:stopOrRefreshSystemItem target:self.webView action:stopOrRefreshAction];
UIBarButtonItem *systemAction = [UIBarButtonItem barButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(systemAction:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:
[UIBarButtonItem flexibleSpaceBarButtonItem],
back,
[UIBarButtonItem flexibleSpaceBarButtonItem],
forward,
[UIBarButtonItem fixedSpaceBarButtonItemWithWidth:(self.view.bounds.size.width / 3.0f)],
stopOrRefresh,
[UIBarButtonItem flexibleSpaceBarButtonItem],
systemAction,
[UIBarButtonItem flexibleSpaceBarButtonItem],
nil];
[self setToolbarItems:toolbarItems];
self.navigationItem.title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
- (void)systemAction:(UIBarButtonItem *)sender {
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:[[self.webView.request URL] absoluteString]
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Action sheet cancel button")
destructiveButtonTitle:nil
otherButtonTitles:
NSLocalizedString(@"Open in Safari", @"Action sheet button"),
nil]
autorelease];
[actionSheet showFromToolbar:self.navigationController.toolbar];
}
- (void)dealloc {
[self setLoading:NO];
[webView stopLoading];
webView.delegate = nil;
[webView release];
webView = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Accessors
- (void)setLoading:(BOOL)newLoadingValue {
if(loading != newLoadingValue) {
if(newLoadingValue) {
[[UIApplication sharedApplication] didStartNetworkRequest];
} else {
[[UIApplication sharedApplication] didStopNetworkRequest];
}
[self refreshToolbars];
}
loading = newLoadingValue;
}
#pragma mark -
#pragma mark UIActionSheetDelegate methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex != actionSheet.cancelButtonIndex) {
NSInteger adjustedIndex = buttonIndex - actionSheet.firstOtherButtonIndex;
switch(adjustedIndex) {
case 0:;
[[UIApplication sharedApplication] openURL:[self.webView.request URL]];
break;
}
}
}
#pragma mark -
#pragma mark UIWebViewDelegate methods
- (void)webViewDidStartLoad:(UIWebView *)aWebView {
if(![aWebView isEqual:self.webView]) {
TLDebugLog(@"Unexpected start webview callback from %@, self.webView is %@", localWebView, self.webView);
return;
}
self.loading = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
if(![aWebView isEqual:self.webView]) {
TLDebugLog(@"Unexpected finish webview callback from %@, self.webView is %@", localWebView, self.webView);
return;
}
self.loading = NO;
}
- (void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error {
if(![aWebView isEqual:self.webView]) {
TLDebugLog(@"Unexpected fail webview callback from %@, self.webView is %@", localWebView, self.webView);
return;
}
BOOL shouldShowAlert = NO;
NSString *alertMessage = nil;
switch([error code]) {
case NSURLErrorCancelled:;
shouldShowAlert = NO;
break;
case NSURLErrorTimedOut:;
case NSURLErrorCannotConnectToHost:;
case NSURLErrorNetworkConnectionLost:;
case NSURLErrorResourceUnavailable:;
case NSURLErrorNotConnectedToInternet:;
case NSURLErrorCannotLoadFromNetwork:;
shouldShowAlert = YES;
alertMessage = NSLocalizedString(@"Couldn't connect.", @"Message when page load failed due to non-connectivity");
break;
case NSURLErrorDataLengthExceedsMaximum:;
shouldShowAlert = YES;
alertMessage = NSLocalizedString(@"Page too big to handle.", @"Message when page load failed due to data size");
break;
case NSURLErrorBadURL:;
case NSURLErrorUnsupportedURL:;
shouldShowAlert = YES;
alertMessage = NSLocalizedString(@"Unsupported url.", @"Message when page load failed due to bad url");
break;
case NSURLErrorSecureConnectionFailed:;
case NSURLErrorServerCertificateHasBadDate:;
case NSURLErrorServerCertificateUntrusted:;
case NSURLErrorServerCertificateHasUnknownRoot:;
case NSURLErrorServerCertificateNotYetValid:;
shouldShowAlert = YES;
alertMessage = NSLocalizedString(@"Secure connection failed.", @"Generic message for security / certificate failures");
break;
case NSURLErrorCannotCreateFile:;
case NSURLErrorCannotOpenFile:;
case NSURLErrorCannotCloseFile:;
case NSURLErrorCannotWriteToFile:;
case NSURLErrorCannotRemoveFile:;
case NSURLErrorCannotMoveFile:;
case NSURLErrorFileDoesNotExist:;
case NSURLErrorFileIsDirectory:;
shouldShowAlert = YES;
alertMessage = NSLocalizedString(@"Can't load file urls.", @"Generic message for file errors (file errors?!)");
break;
case NSURLErrorUnknown:;
case NSURLErrorHTTPTooManyRedirects:;
case NSURLErrorCannotFindHost:;
case NSURLErrorDNSLookupFailed:;
case NSURLErrorRedirectToNonExistentLocation:;
case NSURLErrorBadServerResponse:;
case NSURLErrorZeroByteResource:;
case NSURLErrorUserCancelledAuthentication:;
case NSURLErrorClientCertificateRejected:;
case NSURLErrorUserAuthenticationRequired:;
case NSURLErrorNoPermissionsToReadFile:;
case NSURLErrorDownloadDecodingFailedMidStream:;
case NSURLErrorDownloadDecodingFailedToComplete:;
default:;
shouldShowAlert = YES;
alertMessage = NSLocalizedString(@"Couldn't load that page.", @"Generic message when page load failed");
break;
}
if(shouldShowAlert && alertMessage) {
[UIAlertView showAlertWithTitle:NSLocalizedString(@"Whoops", @"Title when page load failed")
message:alertMessage];
}
self.loading = NO;
}
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(![aWebView isEqual:self.webView]) {
TLDebugLog(@"Unexpected shouldStart webview callback from %@, self.webView is %@", localWebView, self.webView);
return NO;
}
if(![UIWebView shouldOpenURLInBrowser:[request URL]]) {
[[UIApplication sharedApplication] openURL:[request URL]];
}
return YES;
}
@end