-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChildBrowserViewController.m
executable file
·228 lines (190 loc) · 7.2 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
// / Created by Jesse MacFadyen on 10-05-29.
// Copyright 2010 Nitobi. All rights reserved.
// Copyright 2012, Randy McMillan
#import "ChildBrowserViewController.h"
@implementation ChildBrowserViewController
@synthesize imageURL, isImage;
@synthesize delegate, orientationDelegate;
@synthesize spinner, webView, addressLabel;
@synthesize closeBtn, refreshBtn, backBtn, fwdBtn, safariBtn;
/*
// 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;
}
*/
+ (NSString*)resolveImageResource:(NSString*)resource
{
NSString* systemVersion = [[UIDevice currentDevice] systemVersion];
BOOL isLessThaniOS4 = ([systemVersion compare:@"4.0" options:NSNumericSearch] == NSOrderedAscending);
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; // if all else fails
}
- (ChildBrowserViewController*)initWithScale:(BOOL)enabled
{
self = [super init];
self.scaleEnabled = enabled;
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
self.refreshBtn.image = [UIImage imageNamed:[[self class] resolveImageResource:@"ChildBrowser.bundle/but_refresh"]];
self.backBtn.image = [UIImage imageNamed:[[self class] resolveImageResource:@"ChildBrowser.bundle/arrow_left"]];
self.fwdBtn.image = [UIImage imageNamed:[[self class] resolveImageResource:@"ChildBrowser.bundle/arrow_right"]];
self.safariBtn.image = [UIImage imageNamed:[[self class] resolveImageResource:@"ChildBrowser.bundle/compass"]];
self.webView.delegate = self;
self.webView.scalesPageToFit = TRUE;
self.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
{
self.webView.delegate = nil;
self.delegate = nil;
self.orientationDelegate = nil;
#if !__has_feature(objc_arc)
self.webView = nil;
self.closeBtn = nil;
self.refreshBtn = nil;
self.addressLabel = nil;
self.backBtn = nil;
self.fwdBtn = nil;
self.safariBtn = nil;
self.spinner = nil;
[super dealloc];
#endif
}
- (void)closeBrowser
{
if (self.delegate != nil) {
[self.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
{
[self closeBrowser];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]];
[self.webView loadRequest:request];
}
- (IBAction)onSafariButtonPress:(id)sender
{
if (self.delegate != nil) {
[self.delegate onOpenInSafari];
}
if (self.isImage) {
NSURL* pURL = [NSURL URLWithString:self.imageURL];
[[UIApplication sharedApplication] openURL:pURL];
} else {
NSURLRequest* request = self.webView.request;
[[UIApplication sharedApplication] openURL:request.URL];
}
}
- (void)loadURL:(NSString*)url
{
NSLog(@"Opening Url : %@", url);
if ([url hasSuffix:@".png"] ||
[url hasSuffix:@".jpg"] ||
[url hasSuffix:@".jpeg"] ||
[url hasSuffix:@".bmp"] ||
[url hasSuffix:@".gif"]) {
self.imageURL = nil;
self.imageURL = url;
self.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];
[self.webView loadHTMLString:htmlText baseURL:[NSURL URLWithString:@""]];
} else {
self.imageURL = @"";
self.isImage = NO;
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[self.webView loadRequest:request];
}
self.webView.hidden = NO;
}
- (void)webViewDidStartLoad:(UIWebView*)sender
{
self.addressLabel.text = @"Loading...";
self.backBtn.enabled = self.webView.canGoBack;
self.fwdBtn.enabled = self.webView.canGoForward;
[self.spinner startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView*)sender
{
NSURLRequest* request = self.webView.request;
NSLog(@"New Address is : %@", request.URL.absoluteString);
self.addressLabel.text = request.URL.absoluteString;
self.backBtn.enabled = self.webView.canGoBack;
self.fwdBtn.enabled = self.webView.canGoForward;
[self.spinner stopAnimating];
if (self.delegate != NULL) {
[self.delegate onChildLocationChange:request.URL.absoluteString];
}
}
/*- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error {
NSLog (@"webView:didFailLoadWithError");
[spinner stopAnimating];
addressLabel.text = @"Failed";
if (error != NULL) {
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle: [error localizedDescription]
message: [error localizedFailureReason]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
}
*/
#pragma mark CDVOrientationDelegate
- (BOOL)shouldAutorotate
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotate)]) {
return [self.orientationDelegate shouldAutorotate];
}
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.orientationDelegate supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) {
return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
return YES;
}
@end