-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRPWebViewController.m
More file actions
219 lines (175 loc) · 7.03 KB
/
Copy pathPRPWebViewController.m
File metadata and controls
219 lines (175 loc) · 7.03 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
/***
* Excerpted from "iOS Recipes",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/cdirec for more book information.
***/
//
// PPRWebViewController.m
//
// Created by Matt Drance on 6/30/10.
// Copyright 2010 Bookhouse. All rights reserved.
//
#import "PRPWebViewController.h"
const float PRPWebViewControllerFadeDuration = 0.5;
@interface PRPWebViewController ()
- (void)fadeWebViewIn;
- (void)resetBackgroundColor;
@end
@implementation PRPWebViewController
@synthesize url;
@synthesize backgroundColor;
@synthesize webView;
@synthesize activityIndicator;
@synthesize showsDoneButton;
@synthesize delegate;
- (void)dealloc {
[url release], url = nil;
[backgroundColor release], backgroundColor = nil;
[webView stopLoading], webView.delegate = nil, [webView release], webView = nil;
[activityIndicator release], activityIndicator = nil;
[super dealloc];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.webView = nil;
self.activityIndicator = nil;
}
- (void)loadView {
UIViewAutoresizing resizeAllMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
mainView.autoresizingMask = resizeAllMask;
self.view = mainView;
[mainView release];
//AARON ADDED THIS
self.url = [NSURL URLWithString:@"http://www.pragprog.com/titles/cdirec/ios-recipes"];
self.showsDoneButton = YES;
self.delegate = self;
self.backgroundColor = [UIColor colorWithRed:0.151 green:0.151 blue:0.151 alpha:1.000];
webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.autoresizingMask = resizeAllMask;
webView.scalesPageToFit = YES;
webView.delegate = self;
[self.view addSubview:webView];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin;
CGRect aiFrame = self.activityIndicator.frame;
CGFloat originX = (self.view.bounds.size.width - aiFrame.size.width) / 2;
CGFloat originY = (self.view.bounds.size.height - aiFrame.size.height) / 2;
aiFrame.origin.x = floorl(originX);
aiFrame.origin.y = floorl(originY);
self.activityIndicator.frame = aiFrame;
[self.view addSubview:activityIndicator];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self resetBackgroundColor];
[self reload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
BOOL shouldRotate = YES;
if ([self.delegate respondsToSelector:@selector(webController:shouldAutorotateToInterfaceOrientation:)]) {
shouldRotate = [self.delegate webController:self shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
return shouldRotate;
}
#pragma mark -
#pragma mark Actions
- (void)doneButtonTapped:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)fadeWebViewIn {
[UIView animateWithDuration:PRPWebViewControllerFadeDuration
animations:^ {
self.webView.alpha = 1.0;
}];
}
- (void)reload {
if (self.url) {
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
self.webView.alpha = 0.0;
[self.activityIndicator startAnimating];
}
}
#pragma mark -
#pragma mark Accessor overrides
- (void)setShowsDoneButton:(BOOL)shows {
if (showsDoneButton != shows) {
showsDoneButton = shows;
if (showsDoneButton) {
UIBarButtonItem *done = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneButtonTapped:)];
self.navigationItem.rightBarButtonItem = done;
[done release];
} else {
self.navigationItem.rightBarButtonItem = nil;
}
}
}
- (void)setBackgroundColor:(UIColor *)color {
if (backgroundColor != color) {
[backgroundColor release];
backgroundColor = [color retain];
[self resetBackgroundColor];
}
}
- (void)resetBackgroundColor {
if ([self isViewLoaded]) {
UIColor *bgColor = self.backgroundColor;
if (bgColor == nil) {
bgColor = [UIColor whiteColor];
}
self.view.backgroundColor = bgColor;
}
}
#pragma mark -
#pragma mark UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)wv {
[self.activityIndicator stopAnimating];
[self fadeWebViewIn];
if (self.title == nil) {
NSString *docTitle = [self.webView
stringByEvaluatingJavaScriptFromString:@"document.title;"];
if ([docTitle length] > 0) {
self.navigationItem.title = docTitle;
}
}
SEL sel_didFinishLoading = @selector(webControllerDidFinishLoading:);
if ([self.delegate respondsToSelector:sel_didFinishLoading]) {
[self.delegate webControllerDidFinishLoading:self];
}
}
- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error {
NSLog(@"webView fail: %@", error);
[self.activityIndicator stopAnimating];
if ([self.delegate respondsToSelector:@selector(webController:didFailLoadWithError:)]) {
[self.delegate webController:self didFailLoadWithError:error];
} else {
if ([error code] != kCFURLErrorCancelled) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Load Failed"
message:@"The web page failed to load."
delegate:nil cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
#pragma mark - PRPWebViewControllerDelegate
- (void)webControllerDidFinishLoading:(PRPWebViewController *)controller {
NSLog(@"webControllerDidFinishLoading!");
}
- (void)webController:(PRPWebViewController *)controller didFailLoadWithError:(NSError *)error {
[[[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] autorelease] show];
}
- (BOOL)webController:(PRPWebViewController *)controller shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return [self shouldAutorotateToInterfaceOrientation:orientation];
}
@end