Skip to content

Commit 1b64073

Browse files
committed
Added goBack and goForward methods
1 parent a3565f8 commit 1b64073

3 files changed

Lines changed: 147 additions & 176 deletions

File tree

AQWebView/AQWebView.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@
1010
- (void)reload;
1111
- (void)goBack;
1212
- (void)goForward;
13-
- (BOOL)canGoBack;
14-
- (BOOL)canGoForward;
1513

1614
@end

AQWebView/AQWebView.m

Lines changed: 146 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -16,60 +16,60 @@ @interface AQWebView () <RCTAutoInsetsProtocol, WKNavigationDelegate>
1616

1717
@implementation AQWebView
1818
{
19-
WKWebView *_webView;
20-
UIActivityIndicatorView *_spinner;
21-
UIRefreshControl *_refreshControl;
19+
WKWebView *_webView;
20+
UIActivityIndicatorView *_spinner;
21+
UIRefreshControl *_refreshControl;
2222
}
2323

2424
- (instancetype)initWithFrame:(CGRect)frame
2525
{
26-
if ((self = [super initWithFrame:frame])) {
27-
super.backgroundColor = [UIColor clearColor];
28-
_automaticallyAdjustContentInsets = YES;
29-
_contentInset = UIEdgeInsetsZero;
30-
31-
_webView = [[WKWebView alloc] initWithFrame:self.bounds];
32-
_webView.allowsBackForwardNavigationGestures = YES;
33-
34-
// Disabled because it feels less like an app if you can preview links
35-
//_webView.allowsLinkPreview = YES;
36-
37-
_webView.navigationDelegate = self;
38-
[self addSubview:_webView];
39-
40-
_spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
41-
[_spinner setTranslatesAutoresizingMaskIntoConstraints:NO];
42-
[_spinner startAnimating];
43-
[_webView addSubview:_spinner];
44-
45-
/*
46-
_refreshControl = [[UIRefreshControl alloc] init];
47-
[_webView.scrollView addSubview:_refreshControl];
48-
[_refreshControl addTarget:self action:@selector(reload) forControlEvents:UIControlEventValueChanged];
49-
*/
50-
51-
[_webView addConstraint:[NSLayoutConstraint constraintWithItem:_spinner
52-
attribute:NSLayoutAttributeCenterX
53-
relatedBy:NSLayoutRelationEqual
54-
toItem:_webView
55-
attribute:NSLayoutAttributeCenterX
56-
multiplier:1
57-
constant:0]];
58-
59-
[_webView addConstraint:[NSLayoutConstraint constraintWithItem:_spinner
60-
attribute:NSLayoutAttributeCenterY
61-
relatedBy:NSLayoutRelationEqual
62-
toItem:_webView
63-
attribute:NSLayoutAttributeCenterY
64-
multiplier:1
65-
constant:0]];
66-
}
67-
return self;
26+
if ((self = [super initWithFrame:frame])) {
27+
super.backgroundColor = [UIColor clearColor];
28+
_automaticallyAdjustContentInsets = YES;
29+
_contentInset = UIEdgeInsetsZero;
30+
31+
_webView = [[WKWebView alloc] initWithFrame:self.bounds];
32+
_webView.allowsBackForwardNavigationGestures = YES;
33+
34+
// Disabled because it feels less like an app if you can preview links
35+
//_webView.allowsLinkPreview = YES;
36+
37+
_webView.navigationDelegate = self;
38+
[self addSubview:_webView];
39+
40+
_spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
41+
[_spinner setTranslatesAutoresizingMaskIntoConstraints:NO];
42+
[_spinner startAnimating];
43+
[_webView addSubview:_spinner];
44+
45+
/*
46+
_refreshControl = [[UIRefreshControl alloc] init];
47+
[_webView.scrollView addSubview:_refreshControl];
48+
[_refreshControl addTarget:self action:@selector(reload) forControlEvents:UIControlEventValueChanged];
49+
*/
50+
51+
[_webView addConstraint:[NSLayoutConstraint constraintWithItem:_spinner
52+
attribute:NSLayoutAttributeCenterX
53+
relatedBy:NSLayoutRelationEqual
54+
toItem:_webView
55+
attribute:NSLayoutAttributeCenterX
56+
multiplier:1
57+
constant:0]];
58+
59+
[_webView addConstraint:[NSLayoutConstraint constraintWithItem:_spinner
60+
attribute:NSLayoutAttributeCenterY
61+
relatedBy:NSLayoutRelationEqual
62+
toItem:_webView
63+
attribute:NSLayoutAttributeCenterY
64+
multiplier:1
65+
constant:0]];
66+
}
67+
return self;
6868
}
6969

7070
- (void)reload
7171
{
72-
[_webView loadRequest:[NSURLRequest requestWithURL:_webView.URL]];
72+
[_webView loadRequest:[NSURLRequest requestWithURL:_webView.URL]];
7373
}
7474

7575
- (void)goBack
@@ -82,162 +82,152 @@ - (void)goForward
8282
[_webView goForward];
8383
}
8484

85-
- (BOOL)canGoBack
86-
{
87-
return _webView.canGoBack;
88-
}
89-
90-
- (BOOL)canGoForward
91-
{
92-
return _webView.canGoForward;
93-
}
94-
9585
- (void)setSource:(NSDictionary *)source
9686
{
97-
if (![_source isEqualToDictionary:source]) {
98-
_source = [source copy];
99-
100-
// Check for a static html source first
101-
NSString *html = [RCTConvert NSString:source[@"html"]];
102-
if (html) {
103-
NSURL *baseURL = [RCTConvert NSURL:source[@"baseUrl"]];
104-
_userLoadedUrl = [baseURL absoluteString];
105-
[_webView loadHTMLString:html baseURL:baseURL];
106-
return;
107-
}
108-
109-
NSURLRequest *request = [RCTConvert NSURLRequest:source];
110-
// Because of the way React works, as pages redirect, we actually end up
111-
// passing the redirect urls back here, so we ignore them if trying to load
112-
// the same url. We'll expose a call to 'reload' to allow a user to load
113-
// the existing page.
114-
if ([request.URL isEqual:_webView.URL]) {
115-
return;
116-
}
117-
if (!request.URL) {
118-
// Clear the webview
119-
[_webView loadHTMLString:@"" baseURL:nil];
120-
return;
87+
if (![_source isEqualToDictionary:source]) {
88+
_source = [source copy];
89+
90+
// Check for a static html source first
91+
NSString *html = [RCTConvert NSString:source[@"html"]];
92+
if (html) {
93+
NSURL *baseURL = [RCTConvert NSURL:source[@"baseUrl"]];
94+
_userLoadedUrl = [baseURL absoluteString];
95+
[_webView loadHTMLString:html baseURL:baseURL];
96+
return;
97+
}
98+
99+
NSURLRequest *request = [RCTConvert NSURLRequest:source];
100+
// Because of the way React works, as pages redirect, we actually end up
101+
// passing the redirect urls back here, so we ignore them if trying to load
102+
// the same url. We'll expose a call to 'reload' to allow a user to load
103+
// the existing page.
104+
if ([request.URL isEqual:_webView.URL]) {
105+
return;
106+
}
107+
if (!request.URL) {
108+
// Clear the webview
109+
[_webView loadHTMLString:@"" baseURL:nil];
110+
return;
111+
}
112+
_userLoadedUrl = [request.URL absoluteString];
113+
[_webView loadRequest:request];
121114
}
122-
_userLoadedUrl = [request.URL absoluteString];
123-
[_webView loadRequest:request];
124-
}
125115
}
126116

127117
- (void)layoutSubviews
128118
{
129-
[super layoutSubviews];
130-
_webView.frame = self.bounds;
119+
[super layoutSubviews];
120+
_webView.frame = self.bounds;
131121
}
132122

133123
- (void)setContentInset:(UIEdgeInsets)contentInset
134124
{
135-
_contentInset = contentInset;
136-
[RCTView autoAdjustInsetsForView:self
137-
withScrollView:_webView.scrollView
138-
updateOffset:NO];
125+
_contentInset = contentInset;
126+
[RCTView autoAdjustInsetsForView:self
127+
withScrollView:_webView.scrollView
128+
updateOffset:NO];
139129
}
140130

141131
- (void)setBackgroundColor:(UIColor *)backgroundColor
142132
{
143-
CGFloat alpha = CGColorGetAlpha(backgroundColor.CGColor);
144-
self.opaque = _webView.opaque = (alpha == 1.0);
145-
_webView.backgroundColor = backgroundColor;
133+
CGFloat alpha = CGColorGetAlpha(backgroundColor.CGColor);
134+
self.opaque = _webView.opaque = (alpha == 1.0);
135+
_webView.backgroundColor = backgroundColor;
146136
}
147137

148138
- (UIColor *)backgroundColor
149139
{
150-
return _webView.backgroundColor;
140+
return _webView.backgroundColor;
151141
}
152142

153143
- (void)refreshContentInset
154144
{
155-
[RCTView autoAdjustInsetsForView:self
156-
withScrollView:_webView.scrollView
157-
updateOffset:YES];
145+
[RCTView autoAdjustInsetsForView:self
146+
withScrollView:_webView.scrollView
147+
updateOffset:YES];
158148
}
159149

160150
- (NSMutableDictionary<NSString *, id> *)baseEvent
161151
{
162-
NSMutableDictionary<NSString *, id> *event = [[NSMutableDictionary alloc] initWithDictionary:
163-
@{
164-
@"url": [_webView.URL absoluteString] ?: @"",
165-
@"loading" : @(_webView.loading),
166-
@"title": [_webView title] ?: @"",
167-
@"canGoBack": @(_webView.canGoBack),
168-
@"canGoForward" : @(_webView.canGoForward),
169-
}
170-
];
171-
172-
return event;
152+
NSMutableDictionary<NSString *, id> *event = [[NSMutableDictionary alloc] initWithDictionary:
153+
@{
154+
@"url": [_webView.URL absoluteString] ?: @"",
155+
@"loading" : @(_webView.loading),
156+
@"title": [_webView title] ?: @"",
157+
@"canGoBack": @(_webView.canGoBack),
158+
@"canGoForward" : @(_webView.canGoForward),
159+
}
160+
];
161+
162+
return event;
173163
}
174164

175165
#pragma mark - WKNavigationDelegate
176166

177167
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
178168
{
179-
NSURL *url = navigationAction.request.URL;
180-
NSString *urlStr = [url absoluteString];
181-
BOOL block = false;
182-
183-
if ([urlStr isEqual: @""]) {
184-
urlStr = @"about:blank";
185-
}
186-
187-
if (![urlStr isEqual: _userLoadedUrl]) {
188-
for (id prefix in _blockedPrefixes) {
189-
if ([urlStr hasPrefix:prefix]) {
190-
NSMutableDictionary<NSString *, id> *event = [[NSMutableDictionary alloc] initWithDictionary:
191-
@{
192-
@"url": urlStr,
193-
}
194-
];
195-
_onPrefixBlocked(event);
196-
decisionHandler(WKNavigationActionPolicyCancel);
197-
block = true;
198-
break;
199-
}
169+
NSURL *url = navigationAction.request.URL;
170+
NSString *urlStr = [url absoluteString];
171+
BOOL block = false;
172+
173+
if ([urlStr isEqual: @""]) {
174+
urlStr = @"about:blank";
175+
}
176+
177+
if (![urlStr isEqual: _userLoadedUrl]) {
178+
for (id prefix in _blockedPrefixes) {
179+
if ([urlStr hasPrefix:prefix]) {
180+
NSMutableDictionary<NSString *, id> *event = [[NSMutableDictionary alloc] initWithDictionary:
181+
@{
182+
@"url": urlStr,
183+
}
184+
];
185+
_onPrefixBlocked(event);
186+
decisionHandler(WKNavigationActionPolicyCancel);
187+
block = true;
188+
break;
189+
}
190+
}
191+
}
192+
193+
if (!block) {
194+
decisionHandler(WKNavigationActionPolicyAllow);
200195
}
201-
}
202-
203-
if (!block) {
204-
decisionHandler(WKNavigationActionPolicyAllow);
205-
}
206196
}
207197

208198
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
209199
{
210-
[_spinner stopAnimating];
211-
[_refreshControl endRefreshing];
212-
213-
if (_onLoadingFinish) {
214-
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
215-
_onLoadingFinish(event);
216-
}
200+
[_spinner stopAnimating];
201+
[_refreshControl endRefreshing];
202+
203+
if (_onLoadingFinish) {
204+
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
205+
_onLoadingFinish(event);
206+
}
217207
}
218208

219209
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
220210
{
221-
if (_onLoadingStart) {
222-
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
223-
_onLoadingStart(event);
224-
}
211+
if (_onLoadingStart) {
212+
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
213+
_onLoadingStart(event);
214+
}
225215
}
226216

227217
-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
228218
{
229-
[_spinner stopAnimating];
230-
[_refreshControl endRefreshing];
231-
232-
if (_onLoadingError) {
233-
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
234-
[event addEntriesFromDictionary:@{
235-
@"domain": error.domain,
236-
@"code": @(error.code),
237-
@"description": error.localizedDescription,
238-
}];
239-
_onLoadingError(event);
240-
}
219+
[_spinner stopAnimating];
220+
[_refreshControl endRefreshing];
221+
222+
if (_onLoadingError) {
223+
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
224+
[event addEntriesFromDictionary:@{
225+
@"domain": error.domain,
226+
@"code": @(error.code),
227+
@"description": error.localizedDescription,
228+
}];
229+
_onLoadingError(event);
230+
}
241231
}
242232

243233
@end

0 commit comments

Comments
 (0)