This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLCWebBrowserViewController.m
212 lines (155 loc) · 7.56 KB
/
BLCWebBrowserViewController.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
//
// BLCWebBrowserViewController.m
// BlocBrowser
//
// Created by Douglas Hewitt on 4/19/15.
// Copyright (c) 2015 Bloc. All rights reserved.
//
#import "BLCWebBrowserViewController.h"
#import "BLCAwesomeFloatingToolbar.h"
#define kBLCWebBrowserBackString NSLocalizedString(@"Back", @"Back command")
#define kBLCWebBrowserForwardString NSLocalizedString(@"Forward", @"Forward command")
#define kBLCWebBrowserStopString NSLocalizedString(@"Stop", @"Stop command")
#define kBLCWebBrowserRefreshString NSLocalizedString(@"Refresh", @"Reload command")
@interface BLCWebBrowserViewController () <UIWebViewDelegate, UITextFieldDelegate, BLCAwesomeFloatingToolbarDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicator;
@property (nonatomic, strong) BLCAwesomeFloatingToolbar *awesomeToolbar;
@property (nonatomic, assign) NSUInteger frameCount;
@end
@implementation BLCWebBrowserViewController
- (void) resetWebView {
[self.webView removeFromSuperview];
UIWebView *newWebView = [[UIWebView alloc] init];
newWebView.delegate = self;
[self.view addSubview:newWebView];
self.webView = newWebView;
self.textField.text = nil;
[self updateButtonsAndTitle];
}
#pragma mark - UIViewController
- (void)loadView {
UIView *mainView = [UIView new];
self.webView = [[UIWebView alloc] init];
self.webView.delegate = self;
self.textField = [[UITextField alloc] init];
self.textField.keyboardType = UIKeyboardTypeURL;
self.textField.returnKeyType = UIReturnKeyDone;
self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
self.textField.placeholder = NSLocalizedString(@"Website URL or Search", @"Placeholder text for web browser URL field");
self.textField.backgroundColor = [UIColor colorWithWhite:220/255.0f alpha:1];
self.textField.delegate = self;
self.awesomeToolbar = [[[BLCAwesomeFloatingToolbar alloc] init] initWithFourTitles:@[kBLCWebBrowserBackString, kBLCWebBrowserForwardString, kBLCWebBrowserStopString, kBLCWebBrowserRefreshString]];
self.awesomeToolbar.delegate = self;
for (UIView *viewToAdd in @[self.webView, self.textField, self.awesomeToolbar]) {
[mainView addSubview:viewToAdd];
}
self.view = mainView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
// Do any additional setup after loading the view.
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.activityIndicator];
self.awesomeToolbar.frame = CGRectMake(20, 200, 280, 50);
}
- (void) viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
//calculate some dimensions
static const CGFloat itemHeight = 50;
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
CGFloat browserHeight = height - itemHeight;
//assign the frames
self.textField.frame = CGRectMake(0, 0, width, itemHeight);
self.webView.frame = CGRectMake(0, CGRectGetMaxY(self.textField.frame), width, browserHeight);
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSString *URLString = textField.text;
NSURL *URL = [NSURL URLWithString:URLString];
if (!URL.scheme) {
// user forgot http or https
URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", URLString]];
}
if ([URLString rangeOfString:@" "].location != NSNotFound || [URLString rangeOfString:@"."].location == NSNotFound) {
NSString *URLStringForGoogle = [URLString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?q=%@", URLStringForGoogle]];
}
if (URL) {
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
}
return NO;
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
self.frameCount++;
[self updateButtonsAndTitle];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.frameCount--;
[self updateButtonsAndTitle];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Aw snap!", @"Error title") message:[error localizedDescription] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil, nil];
[alert show];
[self updateButtonsAndTitle];
self.frameCount--;
}
#pragma mark - Miscellaneous
- (void) updateButtonsAndTitle {
NSString *webpageTitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
if (webpageTitle) {
self.title = webpageTitle;
} else {
self.title = self.webView.request.URL.absoluteString;
}
if (self.frameCount > 0) {
[self.activityIndicator startAnimating];
} else {
[self.activityIndicator stopAnimating];
}
[self.awesomeToolbar setEnabled:[self.webView canGoBack] forButtonWithTitle:kBLCWebBrowserBackString];
[self.awesomeToolbar setEnabled:[self.webView canGoForward] forButtonWithTitle:kBLCWebBrowserForwardString];
[self.awesomeToolbar setEnabled:self.frameCount > 0 forButtonWithTitle:kBLCWebBrowserStopString];
[self.awesomeToolbar setEnabled:self.frameCount == 0 && self.webView.request.URL forButtonWithTitle:kBLCWebBrowserRefreshString];
}
#pragma mark - BLCAwesomeFloatingToolbarDelegate
- (void) floatingToolbar:(BLCAwesomeFloatingToolbar *)toolbar didSelectButtonWithTitle:(NSString *)title {
if ([title isEqual:kBLCWebBrowserBackString]) {
[self.webView goBack];
} else if ([title isEqual:kBLCWebBrowserForwardString]) {
[self.webView goForward];
} else if ([title isEqual:kBLCWebBrowserStopString]) {
[self.webView stopLoading];
} else if ([title isEqual:kBLCWebBrowserRefreshString]) {
[self.webView reload];
}
}
- (void) floatingToolbar:(BLCAwesomeFloatingToolbar *)toolbar didTryToPanWithOffset:(CGPoint)offset {
CGPoint startingPoint = toolbar.frame.origin;
CGPoint newPoint = CGPointMake(startingPoint.x + offset.x, startingPoint.y + offset.y);
CGRect potentialNewFrame = CGRectMake(newPoint.x, newPoint.y, CGRectGetWidth(toolbar.frame), CGRectGetHeight(toolbar.frame));
if (CGRectContainsRect(self.view.bounds, potentialNewFrame)) {
toolbar.frame = potentialNewFrame;
}
}
- (void) floatingToolbar:(BLCAwesomeFloatingToolbar *)toolbar didTryToPinchWithScaler:(CGFloat)scaler {
CGPoint startingPoint = toolbar.frame.origin;
CGFloat newWidth = CGRectGetWidth(toolbar.frame) * scaler;
CGFloat newHeight = CGRectGetHeight(toolbar.frame) * scaler;
CGRect potentialScaledFrame = CGRectMake(startingPoint.x, startingPoint.y, newWidth, newHeight);
if (CGRectContainsRect(self.view.bounds, potentialScaledFrame)) {
NSLog(@"Scaled frame size is X:%f Y:%f", potentialScaledFrame.size.width, potentialScaledFrame.size.height);
toolbar.frame = potentialScaledFrame;
}
}
- (void) floatingToolbarDidLongTapButton:(BLCAwesomeFloatingToolbar *)toolbar {
[toolbar colorChooser];
}
@end