forked from phonegap-build/ChildBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChildBrowserViewController.m
327 lines (254 loc) · 8.05 KB
/
ChildBrowserViewController.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
//
// ChildBrowserViewController.m
//
// Created by Jesse MacFadyen on 21/07/09.
// Copyright 2009 Nitobi. All rights reserved.
// Copyright (c) 2011, IBM Corporation
// Copyright 2011, Randy McMillan
//
#import "ChildBrowserViewController.h"
@interface ChildBrowserViewController()
//Gesture Recognizer
- (void)addGestureRecognizer;
@end
@implementation ChildBrowserViewController
@synthesize imageURL;
@synthesize supportedOrientations;
@synthesize isImage;
@synthesize delegate;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
// Add *-72@2x.png images to ChildBrowser.bundle
// Just duplicate and rename - @RandyMcMillan
+ (NSString*) resolveImageResource:(NSString*)resource
{
NSString* systemVersion = [[UIDevice currentDevice] systemVersion];
BOOL isLessThaniOS4 = ([systemVersion compare:@"4.0" options:NSNumericSearch] == NSOrderedAscending);
// the iPad image (nor retina) differentiation code was not in 3.x, and we have to explicitly set the path
if (isLessThaniOS4)
{
return [NSString stringWithFormat:@"%@.png", resource];
} else if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00)
{
return [NSString stringWithFormat:@"%@@2x.png", resource];
}
return resource;
}
- (ChildBrowserViewController*)initWithScale:(BOOL)enabled
{
self = [super init];
if(self!=nil)
{
[self addGestureRecognizer];
}
scaleEnabled = enabled;
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
refreshBtn.image = [UIImage imageNamed:[[self class] resolveImageResource:@"ChildBrowser.bundle/but_refresh"]];
backBtn.image = [UIImage imageNamed:[[self class] resolveImageResource:@"ChildBrowser.bundle/arrow_left"]];
fwdBtn.image = [UIImage imageNamed:[[self class] resolveImageResource:@"ChildBrowser.bundle/arrow_right"]];
safariBtn.image = [UIImage imageNamed:[[self class] resolveImageResource:@"ChildBrowser.bundle/compass"]];
webView.delegate = self;
webView.scalesPageToFit = TRUE;
webView.backgroundColor = [UIColor whiteColor];
NSLog(@"View did load");
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
NSLog(@"View did UN-load");
}
- (void)dealloc {
webView.delegate = nil;
[webView release];
[closeBtn release];
[refreshBtn release];
[addressLabel release];
[backBtn release];
[fwdBtn release];
[safariBtn release];
[spinner release];
[ supportedOrientations release];
[super dealloc];
}
-(void)closeBrowser
{
if(delegate != NULL)
{
[delegate onClose];
}
if ([self respondsToSelector:@selector(presentingViewController)]) {
//Reference UIViewController.h Line:179 for update to iOS 5 difference - @RandyMcMillan
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
} else {
[[self parentViewController] dismissModalViewControllerAnimated:YES];
}
}
-(IBAction) onDoneButtonPress:(id)sender
{
[webView stopLoading];
[ self closeBrowser];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]];
[webView loadRequest:request];
}
-(IBAction) onSafariButtonPress:(id)sender
{
if(delegate != NULL)
{
[delegate onOpenInSafari];
}
if(isImage)
{
NSURL* pURL = [[ [NSURL alloc] initWithString:imageURL ] autorelease];
[ [ UIApplication sharedApplication ] openURL:pURL ];
}
else
{
NSURLRequest *request = webView.request;
[[UIApplication sharedApplication] openURL:request.URL];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
{
BOOL autoRotate = [self.supportedOrientations count] > 1; // autorotate if only more than 1 orientation supported
if (autoRotate)
{
if ([self.supportedOrientations containsObject:
[NSNumber numberWithInt:interfaceOrientation]]) {
return YES;
}
}
return NO;
}
- (void)loadURL:(NSString*)url
{
NSLog(@"Opening Url : %@",url);
if( [url hasSuffix:@".png" ] ||
[url hasSuffix:@".jpg" ] ||
[url hasSuffix:@".jpeg" ] ||
[url hasSuffix:@".bmp" ] ||
[url hasSuffix:@".gif" ] )
{
[ imageURL release ];
imageURL = [url copy];
isImage = YES;
NSString* htmlText = @"<html><body style='background-color:#333;margin:0px;padding:0px;'><img style='min-height:200px;margin:0px;padding:0px;width:100%;height:auto;' alt='' src='IMGSRC'/></body></html>";
htmlText = [ htmlText stringByReplacingOccurrencesOfString:@"IMGSRC" withString:url ];
[webView loadHTMLString:htmlText baseURL:[NSURL URLWithString:@""]];
}
else
{
imageURL = @"";
isImage = NO;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];
}
webView.hidden = NO;
}
- (void)webViewDidStartLoad:(UIWebView *)sender {
addressLabel.text = @"Loading...";
backBtn.enabled = webView.canGoBack;
fwdBtn.enabled = webView.canGoForward;
[ spinner startAnimating ];
}
- (void)webViewDidFinishLoad:(UIWebView *)sender
{
NSURLRequest *request = webView.request;
NSLog(@"New Address is : %@",request.URL.absoluteString);
addressLabel.text = request.URL.absoluteString;
backBtn.enabled = webView.canGoBack;
fwdBtn.enabled = webView.canGoForward;
[ spinner stopAnimating ];
if(delegate != NULL)
{
[delegate onChildLocationChange:request.URL.absoluteString];
}
}
- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error {
NSLog (@"webView:didFailLoadWithError");
NSLog (@"%@", [error localizedDescription]);
NSLog (@"%@", [error localizedFailureReason]);
[spinner stopAnimating];
addressLabel.text = @"Failed";
}
#pragma mark - Disaplying Controls
- (void)resetControls
{
CGRect rect = addressLabel.frame;
rect.origin.y = self.view.frame.size.height-(44+26);
[addressLabel setFrame:rect];
rect=webView.frame;
rect.size.height= self.view.frame.size.height-(44+26);
[webView setFrame:rect];
[addressLabel setHidden:NO];
[toolbar setHidden:NO];
}
- (void)showLocationBar:(BOOL)isShow
{
if(isShow)
return;
//the addreslabel heigth 21 toolbar 44
CGRect rect = webView.frame;
rect.size.height+=(26+44);
[webView setFrame:rect];
[addressLabel setHidden:YES];
[toolbar setHidden:YES];
}
- (void)showAddress:(BOOL)isShow
{
if(isShow)
return;
CGRect rect = webView.frame;
rect.size.height+=(26);
[webView setFrame:rect];
[addressLabel setHidden:YES];
}
- (void)showNavigationBar:(BOOL)isShow
{
if(isShow)
return;
CGRect rect = webView.frame;
rect.size.height+=(44);
[webView setFrame:rect];
[toolbar setHidden:YES];
rect = addressLabel.frame;
rect.origin.y+=44;
[addressLabel setFrame:rect];
}
#pragma mark - Gesture Recognizer
- (void)addGestureRecognizer
{
UISwipeGestureRecognizer* closeRG = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(closeBrowser)];
closeRG.direction = UISwipeGestureRecognizerDirectionLeft;
closeRG.delegate=self;
[self.view addGestureRecognizer:closeRG];
[closeRG release];
}
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
//{
// return YES;
//}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return YES;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
@end